2

I would like to know what hook should I used when the permalink/url of a post is modified. My goal is to get the old permalink and new permalink so I can used it for my plugin. Thanks.

[EDIT] Just want to clarify the question. I would like to get the old and new url in a scenario for example, when a user select one its post in the admin area, and edit the current permalink/url(/helloworld) into a new permalink/url(/helloworld_new) of the selected post. I would like to get the complete url the post being edited.

1

You need to exactly use wp_insert_post_data. This contains array of post data what will be store into database after WordPress has done all of the validation/sanitization.

add_filter('wp_insert_post_data', 'wpse_wp_insert_post_data', 10, 2);
function wpse_wp_insert_post_data($data, $post_attr)
{
    // you get the post_name using $data['post_name'];

    // post id will not be present for the first insert
    // but you can check $post_attr['ID'] to be sure if an ID has been passed.
    // note: $data won't contain post id ever, only the $post_attr will have it

    // if you want to compare the name, you could use -
    if( isset($post_attr['post_name']) 
        && !empty($post_attr['post_name']) 
        && $post_attr['post_name'] != $data['post_name'] )
    {
        // So here you can guess post name has been changed
        // note: $post_attr['post_name'] might come undefined or empty sometime.
        // and $data['post_name'] could also comes up empty, but it will be always defined
    }

    // you do not need to modify anything, so you should return it as it is
    return $data;
}

Hope it helps.

0

The action is update_option_permalink_structure.

The follow example works with this hook.

add_action( 'update_option_permalink_structure' , 'my_custom_function', 10, 2 );
function my_custom_function( $old_value, $new_value ) {

    // test
    var_dump( $old_value );
}

The hook in this context is always update_option_{$option} [or update_option_{$option_name} (deprecated since WordPress 3.6)].

Also related is pre_update_option_{$option} and a hook for the Multisite version update_site_option_{$option} and pre_update_site_option_{$option}.

  • 1
    thanks, but my custom function was not executed. I think this hook is triggered when the permalink option is updated. I'm not sure since I am new to wordpress. In my case I refer to the permalink of a certain post. I have updated the question. Thanks again. – Pelang Jan 7 '14 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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