• Resolved golddave

    (@golddave)


    I’m writing my first plugin in a while so be gentle with me please.

    Anyway, I’m writing a plugin that takes an action after a bloggger publishes a post. For this action to take place I need the post ID. The question is how do capture the ID and execute my action once the blogger clicks publish? The rest of the peices should fall into place once I know this information.

    Thanks,
    dave

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Here’s example code that should help:

    function do_stuff_on_publish($post_id,$post) {
      if ($post->post_type == 'post'
          && $post->post_status == 'publish') {
        // do your stuff here, post just got published
        // $post_id contains the id of the post
        // $post contains the post content and such
      } // end if
    } // end function
    add_action('save_post','do_stuff_on_publish',10,2);

    This will allow you go do stuff when the post actually gets published. If they post with a future date, this won’t happen until the post transitions to published and appears on the page.

    Thread Starter golddave

    (@golddave)

    Is the action save_post or publish_post? What’s the difference between the two?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    The action is save_post. The publish_post action happens before the whole post is saved and such.

    Thread Starter golddave

    (@golddave)

    I get the following error when using your code:

    Warning: Missing argument 2 for do_stuff_on_publish() in /home/golddave/public_html/testblog/wp-content/plugins/facebook2.php on line 18

    Warning: Cannot modify header information – headers already sent by (output started at /home/golddave/public_html/testblog/wp-content/plugins/facebook2.php:18) in /home/golddave/public_html/testblog/wp-includes/pluggable.php on line 331

    The error is on the following line:
    function do_stuff_on_publish($post_id,$post) {

    It doesn’t like it when I pass both $post_id and $post.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Sorry, I’m using trunk code, which is different. Try this instead:

    function do_stuff_on_publish($post_id) {
      $post= get_post($post_id);
      if ($post->post_type == 'post'
          && $post->post_status == 'publish') {
        // do your stuff here, post just got published
        // $post_id contains the id of the post
      } // end if
    } // end function
    add_action('save_post','do_stuff_on_publish');
    Thread Starter golddave

    (@golddave)

    Thanks. That did the trick!

    this was very helpful thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Getting Post ID Upon Publishing a Post?’ is closed to new replies.