4

Is there a way I can run some code from a plugin when a future dated entry goes 'live' from 'pending'? I am trying to send an email update that a new entry has been posted. The related post below helped me with this, but it doesn't seem to cover future dated entries.

Here's what I have so far...

craft()->on('entries.onBeforeSaveEntry', function(Event $event) {
    $entry = $event->params['entry'];

    if ($entry->section->handle !== 'blog') {
        return;
    }
    if ($entry->status !== 'live') {
        return;
    }

    if (!$entry->getContent()->emailSent) {
        // send email
    }

    $entry->setContentFromPost(array('emailSent' => true));
});

Related to: Checking for first time an entry is "published" i.e. saved with a Live status

Thanks!

2 Answers 2

3

There's not an easy way.

As the related to link mentions, there isn't an event or anything that Craft fires when a future post dated entry goes live because the way Craft decides if an entry is "live" or not is if it is enabled (both on a per-locale and global basis), it has a post date in the past and an expiration date that doesn't exist or hasn't been met yet.

There's no actual PHP code that executes when that current time actually passes the post date time.

One semi-hacky way to get around it would be to setup a cronjob that hit a controller action every minute in a custom plugin that and plugin would keep a record of every entry's post date (via the onSaveEntry) event.

That controller action would check the current date/time against its internal record of entry post dates and you could fire off an email for any entries when the post date has passed.

1
  • Thanks Brad! This will get me going in the right direction. Oct 13, 2016 at 12:56
3

I've created a plugin called Published Event does exactly what Brad suggests in his answer. It fires off an event when a entry moves from pending to live.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.