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 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?

share|improve this question
add comment

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 ) 
        return;

    // 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 ) )
            return;

        // 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!

share|improve this answer
    
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 '12 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 '12 at 8:14
add comment

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

The hooks I use it on are

add_action('future_to_publish','123_total_calculator');
add_action('new_to_publish','123_total_calculator');
add_action('draft_to_publish','123_total_calculator');
add_action('publish_post','123_total_calculator');
share|improve this answer
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 '12 at 17:55
add comment

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.