I want to execute a function, myfunction() [hypothetically named], whenever a page is updated in the admin. The function doesn’t do anything to the content that is savedâit just runs an unrelated task elsewhere.
I read that the post_updated
hook is good to use for this, but I cannot seem to get this code to run:
add_action('post_updated', 'myfunction');
function myfunction() {
// check if this is a page
$id = get_the_id();
if ( is_page($id) ) {
// do stuff here
}
}
I’m sure I’m missing something obvious; do I have to pass in/return parameters ($post_ID, $post_after, $post_before,
from what I gather in the WordPress Hooks Database) for it to work? If I just want the ID, do I still have to pass in/return the other two?
You should be able to get the ID of the post through the
$_POST
variable.EDIT: Maybe you should try doing this on the
save_post
action, like so, butsave_post
sometimes runs on other instances (ie. Autosave Drafts), so you’ll want to do some more verifying.save_post
will also return the ID as one of the function arguments so you already have that handy.Just tested this on localhost, works good for me.
Used a bit of code from here and you should read up on the
save_post
action if you decide to try this out!get_the_ID() should work, I use it for a very similar purpose.
The hooks I use it on are