0

I've messed with trying to get scheduled posts to fire a function for months and finally stripped things down to the following code to see what is happening. The problem is that a scheduled post will not show the wp_die.

add_action( 'transition_post_status', function ( $new_status, $old_status, $post )  {

    if( 'publish' == $new_status && 'publish' != $old_status ) {

        wp_die('STOP');
    }
}, 10, 3 );

If I simply immediately click on post then the STOP shows but a scheduled post shows nothing and will publish. I've also tried simply wp_die($post); to see what is returned on a scheduled post and there the wp_die isn't fired.

Even forcing this to 'future' == $old_status does not work.

How can I diagnose why this isn't firing for scheduled posts?

I've tried var_dump as well.

3

By default add_action() only passes one parameter, not all three. Change your function to:

    add_action( 'transition_post_status', function ( $new_status, $old_status, $post )  {

    if( 'publish' == $new_status && 'publish' != $old_status ) {

        wp_die('STOP');
    }
}, 10, 3 );

Edited to add:

The transition_post_status hook fires after the post transition from future to publish has occurred, and calling wp_die() will not display 'STOP' for scheduled posts. Instead you will just prevent the remaining actions in wp_transition_post_status() from being processed.

Future posts are published with background scheduled events via wp_cron, so calling a function like var_dump() or print_r() will not be useful for debugging. Instead try using add_option(), wp_mail(), or XDebug bookmarks: jetbrains.com/phpstorm/help/configuring-xdebug.html

6
  • Yes. Sorry. That was also in the code and I'll fix my OP. I cut it out by accident when stripping out the other calls after the wp_die. But the problem remains that wp_die shows for immediate posts but not scheduled posts. The question is how to diagnose the problem.
    – LPH
    Feb 15 '15 at 19:04
  • The transition_post_status hook fires after the post transition from future to publish has occurred, and calling wp_die() will not display 'STOP' for scheduled posts, but instead will just prevent the remaining actions in wp_transition_post_status from being processed. Feb 15 '15 at 19:38
  • Future posts are published with background scheduled events via wp_cron, so calling a function like var_dump, etc will not be useful for debugging. Instead try using add_option(), wp_mail(), or XDebug bookmarks: jetbrains.com/phpstorm/help/configuring-xdebug.html Feb 15 '15 at 19:48
  • Thank you -- I'll look into add_option. Would you modify your answer so that I can mark it? You've been very helpful but initially didn't answer. Your comments however were helpful.
    – LPH
    Feb 16 '15 at 19:59
  • @LPH - I have updated my answer. Feb 17 '15 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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