WordPress.org

Ready to get started?Download WordPress

Forums

Can the publish_post hook be sensitve to future time stamps? (13 posts)

  1. Glenn Ansley
    Member
    Posted 7 years ago #

    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

  2. Glenn Ansley
    Member
    Posted 7 years ago #

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

  3. obscure
    Member
    Posted 7 years ago #

    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.

  4. Glenn Ansley
    Member
    Posted 7 years ago #

    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).

  5. Chris_K
    Member
    Posted 7 years ago #

    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.

  6. Glenn Ansley
    Member
    Posted 7 years ago #

    Thanks HandySolo. I'll do that.

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  8. Glenn Ansley
    Member
    Posted 7 years ago #

    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.

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  10. Glenn Ansley
    Member
    Posted 7 years ago #

    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.

  11. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    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.

  12. Glenn Ansley
    Member
    Posted 7 years ago #

    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:

    http://wordpress.pastebin.ca/447007

  13. Glenn Ansley
    Member
    Posted 7 years ago #

    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 = "glenn@deeperdevotion.com";
    	$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);
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic