I have two page

  1. bar page at url: http://www.example.com/bar/
  2. foo page at url: http://www.example.com/foo/

bar page link to foo page in content, eg.:

<a href="http://www.example.com/foo/">foo</a>

now, i edit the slug for foo page, eg.:


now the link in bar page is wrong and cause a 404 error.

My idea is: when a user change the slug of a page, i replace the old slug with the new in the database, eg. (pseudocode):

// Replace in every posts the old slug with the new
add_action('update_slug', 'my_update_slug', 10, 2);
function my_update_slug($old_slug, $new_slug){
    $sql = "UPDATE wordpress.wp_posts 
            SET post_content = replace(post_content, '$old_slug', '$new_slug');";
    // ...

a bit of question...

  1. Wordpress has some hook for catch the update of slug?
  2. Exists other way to make this?

Sorry for my (poor/beginner's/basic) English.

  • I don't understand your question. If user edits the slug and save the page, the slug is already saved in database (unless the user is a contributor; only authors, editors and admins can edit slugs). What you should update are the links pointed to the old slug or redirect the requests with the old slug to the new one (usually WordPress takes care of this redirection, it is not working for you?).
    – cybmeta
    Jun 22, 2016 at 8:28
  • @cybmeta I would update the old url saved in the other post content; WordPress takes care of this redirection?! not work for me...
    – ar099968
    Jun 22, 2016 at 8:35
  • 1
    Sorry, I was wrong. WordPress takes care of the redirection only for posts by default, not for pages.
    – cybmeta
    Jun 22, 2016 at 8:43
  • One way to do this is have WP insert the page ID instead of the slug when adding links. See How can I store page ID in a post instead of other selected permalink? That way no matter what the slug changes to the links won't break.
    – j08691
    Jun 28, 2016 at 19:37


Your Answer

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

Browse other questions tagged or ask your own question.