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.
Is the action save_post or publish_post? What’s the difference between the two?
The action is save_post. The publish_post action happens before the whole post is saved and such.
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.
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');
Thanks. That did the trick!
735852
this was very helpful thanks