• Hi,
    Is there an action hook like publish_post that takes future timestamps into consideration? It seems that the publish_post processes its action regardless of when the timestamp is set for.

    Thanks

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter Glenn Ansley

    (@blepoxp)

    I’m still looking into this if anyone happens to come across the thread.

    What version of WP are you using? I am running 2.1.2 and posts with a future time stamp are not published until that time arrives.

    Thread Starter Glenn Ansley

    (@blepoxp)

    I’m using 2.1.2 as well. The future post works find when posting to the blog. I was trying to create or modify a plugin that would email when the post was published. Right now, all the plugins that I’ve looked at and all the documentation I’ve read on the hooks they use don’t seem to take that into consideration.

    For example, If I wrote a post today and scheduled it to be published tomorrow, as soon as I select publish the hook is called and the email is sent (even though the post isn’t actually published to the blog until tomorrow).

    Off the cuff, and with really no research, I’d say “no”.

    However, this might be a good one to try asking over at the WP-Hackers list.

    Thread Starter Glenn Ansley

    (@blepoxp)

    Thanks HandySolo. I’ll do that.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    When a future post is published, the “publish_future_post” hook is activated, and the parameter passed is the Post ID number.

    So this would catch a future post being published:

    function future_post_being_published($post_id)
    {
    $post = get_post($post_id);
    // do whatever you like with $post, email it, whatever
    }
    add_action('publish_future_post', 'future_post_being_published', 10, 1);

    Since this is the same hook that the cron job uses to actually publish the post, I cannot guarantee order. When you check, the post may or may not have been published yet, but regardless, it will be published when this hook gets activated.

    Thread Starter Glenn Ansley

    (@blepoxp)

    Hey Otto,
    That doesn’t seem to work. The post will wait to be published correctly, but the email if never sent. I’ll pursue this on the list mentioned above if you don’t have any more suggestions.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Well, that’s really strange, because it worked for me when I tested it.

    Make sure you’re putting this code into a plugin or the theme’s functions.php file, so it will get loaded properly. If your hook isn’t triggering, then your hook is not loading.

    Thread Starter Glenn Ansley

    (@blepoxp)

    Thanks for the quick reply.
    It is in a plugin. The only thing I change in the plugin is to add ‘future’ to the publish_post action as you reference above. Without it, it works (but immediately). With it, it doesn’t work at all.

    Should I post the plugin here for you to look at? it’s about 30 lines long if I leave out the commented section at the top.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Well, I don’t know what to tell you. Check your spelling, perhaps?

    publish_future_post *is* the correct action hook.

    -You can see where it gets scheduled in the wp_insert_post() function in wp-admin/post.php. This makes wp-cron run it when its scheduled time comes around.
    -You can see it being hooked in wp-include/default-filters.php. This is what makes it call wp_publish_post() to actually change the post from future to publish.

    So I really have no idea what you’re doing wrong.

    Note that wp-cron doesn’t guarantee timing. It will only trigger when somebody loads a page from the site after the scheduled time occurs.

    Thread Starter Glenn Ansley

    (@blepoxp)

    That’s so strange, as I said, the only difference is adding “future” to the hook. If you have the time, here’s the code on pastebin:

    https://wordpress.pastebin.ca/447007

    Thread Starter Glenn Ansley

    (@blepoxp)

    Does anybody have a clue as to why this isn’t working? I’m afraid I’ve just been staring at it for too long. The email never gets sent. Thanks.

    function ddmag_devotion_emailer($post_ID) {
    	global $wpdb;
    
    	$email = "[email protected]";
    	$post = $wpdb->get_row( "SELECT post_date, post_title, post_content FROM $wpdb->posts WHERE ID = " . $post_ID );
    	$content = $post->post_content;
    
    	$blogname = get_settings( "blogname" );
    	$subject = "DEEPERDEVOTION.com- " . $post->post_date;
    	$text = "Test Content" ;
    
    	mail( $email,
    		  $subject,
    		  $text,
    		  "From: $email\nContent-Type: text/html;charset=utf-8"
    		);
    }
    
    add_action('publish_future_post', 'ddmag_devotion_emailer', 10, 1);
    ?>
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Can the publish_post hook be sensitve to future time stamps?’ is closed to new replies.