i try to hook when post is updated but all hook i try never executed except updated_post_meta
add_action('updated_post_meta', 'my_function');
function my_function($post_id) {
echo 'This is my post ID : '.$post_id;
}
I’ve tried this add_action('save_post', 'my_function');
but no id was echo out, or maybe this message already echo but never renders because of redirect header is sent.
When a post is updated there are some hooks that are fired:
'pre_post_update'
is an action fired just before the post is updated, the argument passed are 2:$post_ID
and$data
that is an array of all the other database colums of the post table'transition_post_status'
is an hook fired on update, and pass 3 arguments: $new_post_status,$old_post_status
and$post
(object)."{$old_status}_to_{$new_status}"
and"{$new_status}_{$post->post_type}"
. First pass the only the post object as argument, the second pass the post id and the post object. Find documentation here.'edit_post'
that pass 2 arguments:$post_ID
and$post
(object)'post_updated'
that pass 3 arguments:$post_ID
,$post_after
(post object after the update),$post_before
(post object before the update)"save_post_{$post->post_type}"
that depends on post type, e.g. for standard posts is'save_post_post'
and for pages is'save_post_page'
, this hook pass 3 arguments:$post_ID
,$post
(object) and$update
that is a boolean (true or false) that is true when you perform an update, in fact this hook is fired also when a post is saved for first time.save_post
‘ that is fired both on update and on first saving, and pass the same 3 arguments of the previous hook.save_post_{$post_type}
‘ that is fired both on update and on first saving, and pass the same first 2 arguments of the previous hook.wp_insert_post
‘, that is fired both on update and on first saving, and pass the same 3 arguments of the previous 2 hooks.These hook are fired every time a post is updated, both via admin pages in backend and via when updated “manually” using
wp_update_post
orwp_insert_post
functions.When the post is updated using admin pages there are additional hooks fired, an example is
'update_post_redirect'
or'post_updated_messages'
. (See this and this WPSE answers for usage examples).Note that if you want make use of some hooks argument, that isn’t the first, one you have to explicitly declare it in
add_action
call.E.g. if you want to use the
'$update'
argument (that is the 3rd) of the'save_post'
hook you need to add3
as$accepted_args
param onadd_action
(see docs):Last note regard timing: you must be sure that
add_action
is called before the action is triggered, or it will do nothing.E.g. this code:
will do nothing, because the action is added after the hook is fired. Here is simple to recognize it, in real world code isn’t always so.
Why not hook in
post_updated_messages
. That way you can show this message just like the default wordpress post updated.Look for an example here:
http://codex.wordpress.org/Function_Reference/register_post_type
under
post_updated_messages