If somewhere in the code for a WordPress site there is a filter hooked to post_link that’s replacing the URL so that it no longer contains the %postname% placeholder, WordPress will assume there’s nothing to edit. It will therefore output the permalink on the post edit screen without the edit button next to it, and the permalink’s slug won’t be editable.

For an example of how to fix this, see the filters below. The top function one keeps the %postname% placeholder if it exists in the URL, or else uses the Post Name if there is one, or else uses the Post Title. The second function is to fix the post preview URLs so that they go to the correct preview URL rather than the one that the top function outputs.

In order to use these, you’ll need to change quite a bit in order to target the post types you want in both, and build the URLs the way you want them in the top function. In order for the custom URLs to work on the site, you’ll also likely need to implement a new URL rewrite rule using add_rewrite_rule().

2 comments

  1. C. Spencer Beggs March 2, 2017 at 10:23 am

    Reply

    Kellen, thank you so much for this article. That is a crazy-making bug if it crops up on a site.

  2. James White April 6, 2017 at 4:01 pm

    Reply

    This was a very helpful pair of function, thanks very much for sharing! I used the main post link filter on a custom post type, and I noticed that the %postname% placeholder varies based on post type. So, if you have a custom post type “book”, you need to set the slug to end in %book% to preserve the edit buttons. Commenting in case this is of use to another reader.

Leave a reply

Your email address will not be published. Required fields are marked *