Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I can't get my function to trigger on post update or save. I've found a couple of other forum posts around with the same problem, but never an answer :(

My code is as simple as:

function pdfgenerator_update($post_ID) {
    global $MYTEST;
    $MYTEST["pageID"] = $post_ID;         
}
//add_action('trashed_post','pdfgenerator_update');
//add_action('publish_page','pdfgenerator_update');
//add_action('save_post','pdfgenerator_update');
//add_action('edit_post','pdfgenerator_update');
add_action('publish_post','pdfgenerator_update');
add_action('edit_post','pdfgenerator_update');
//add_action('edit_page_form','pdfgenerator_update');

global $MYTEST
$MYTEST["Am I running"] = "YIPPEE";

(N.b. $MYTEST is an array I use to collect debug info which I then print_r in the admin footer.)

As you can see, I've tried a number of different hooks. The only one I ever got working was the edit_page_form... but that didn't seem to pass in any parameters. And I do definitely need the parameters. Also, there's a point of principle. If the correct hook is publish_posts then I damn well want to know why it fails for me.

The code is in my main plugin file, and that is demonstrably being loaded as the YIPPEE is printing correctly.

It seems absurd that this isn't working. I've found so many tutorials and examples exactly like this. I'm frankly getting a little ape here. It should just work! What am I missing?

Many thanks for your help. Alastair

share|improve this question

1 Answer 1

up vote 2 down vote accepted

When a post is published/saved, it does not do it in a single script execution. It redirects to a different script, does the save, then redirects back to where you started. If you want to confirm that your hook is working, use update_option(), and delete the option immediately after displaying it so it's not latent (alternatively: you can make the option count up or something, there's quite a bit you could do, be creative).

Also, the hook you're looking for is save_post, it fires on both wp_insert_post() and wp_update_post() (which are actually both running through wp_insert_post() anyways, just FWIW).

share|improve this answer
    
Thanks, got it working :) –  Alastair Feb 27 '12 at 18:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.