You can do this using the transition_post_status action.
http://codex.wordpress.org/Post_Status_Transitions
This action is not well documented, but you can use it to execute a block of code whenever a post transitions from one status to another (like going from ‘draft’ to ‘publish’). Something like this:
function insert_stock_quote($new_status, $old_status, $post) {
if ( 'publish' == $new_status && !in_array($old_status, array( 'publish', 'private', 'trash' )) ) {
[code to add stock quote goes here]
}
}
add_action('transition_post_status', 'insert_stock quote', 10, 3);
Wouldn’t that only display text when I update the post then?
For example:
function insert_stock_quote($new_status, $old_status, $post) {
if ( ‘publish’ == $new_status && !in_array($old_status, array( ‘publish’, ‘private’, ‘trash’ )) ) {
echo get_quote();
}
}
add_action(‘transition_post_status’, ‘insert_stock quote’, 10, 3);
Also where would I write this code to have it generate static content?
I’m a Newb at this so Thanks for your patience.
John
Try adding it to your theme’s functions.php file.
Ok so I added this to functions, notice that I just used an echo for testing.
function insert_stock_quote($new_status, $old_status, $post) {
if ( 'publish' == $new_status && !in_array($old_status, array( 'publish', 'private', 'trash' )) ) {
echo 'this goes at the top of the post';
}
}
add_action('transition_post_status', 'insert_stock quote', 10, 3);
and got these errors when I published or when I hit upgrade, and it doesn’t work:
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'insert_stock quote' was given in /home/bhgadmin/public_html/vannet/wp-includes/plugin.php on line 395
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'insert_stock quote' was given in /home/bhgadmin/public_html/vannet/wp-includes/plugin.php on line 395
Warning: Cannot modify header information - headers already sent by (output started at /home/bhgadmin/public_html/vannet/wp-includes/plugin.php:395) in /home/bhgadmin/public_html/vannet/wp-includes/pluggable.php on line 890
The first two errors are the result of a typo (my mistake). There should be an underscore between ‘stock’ and ‘quote’ in the function name:
add_action('transition_post_status', 'insert_stock_quote', 10, 3);
As for the ‘headers already sent’ error, you can’t just echo the text to the screen like that. You need to write a function that inserts the text into the WordPress database. My only purpose in posting that code snippet was to show how you could plug the function into WordPress using an appropriate action.
Ok thanks, that makes sense.
Could you perhaps direct me to how I might learn to do that? Or post a sample? (writing to the WP database).
Thanks for your help, it’s MUCH appreciated.
IH