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?

2 Answers 2


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, but save_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.

add_action('save_post', 'myfunction');

function myfunction( $id ) {

    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 

    // check if this is a page
    if ( 'page' == $_POST['post_type'] ) {

        // Return nothing if the user can't edit this page
        if ( !current_user_can( 'edit_page', $id ) )

        // do stuff here, we have verified this is a page and you can edit it



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!

  • This doesn't work for me. Things I have double-checked: The function works if I change the conditional to 1 == 1. The post I'm testing with is a page.
    – supertrue
    Jan 25, 2012 at 7:55
  • @supertrue What exactly are you trying to do? Just return the ID of the page that was updated? See my edit above.
    – Jared
    Jan 25, 2012 at 8:14

get_the_ID() should work, I use it for a very similar purpose.

The hooks I use it on are

  • 1
    Not trying to hijack this but I tried running get_the_ID() on add_action('edit_post', myFunction) and it failed. Any luck with you?
    – AlxVallejo
    Jan 25, 2012 at 17:55
  • @AlxVallejo get_the_id() worked for me using the wp_insert_post_data action hook/filter with priority 99 set on a post, both updating existing and a new one. Only 5 years later, but good for other to reference in the future. ;) Aug 1, 2017 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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