I need to rewrite some URLs to custom templates. For example, I have a page at domain.com/page, and when a user clicks 'remove' on an element, it goes to domain.com/page/?remove=54. I'd like it to rewrite to domain.com/page/remove/54.

Help is appreciated, thank you in advance!

  • Hi @Gavin: Can you clarify your question? Where is this 'remove' that the user clicks on? What code is creating the link? Is your code creating this link? What are you removing? (I know it's not "important" for the question but it helps me understand what you are trying to do.) OR are you saying you want to have URLs of the format /page/remove/54 and you want the 54 to be accessible in your theme using $_GET['remove'] on the page normally loaded by the /page/ URL? Oct 6 '10 at 7:30
  • There are several functions that are going to be using url variables - for instance, one removes an event created by a custom plugin. User clicks an 'x', and the link goes to ?remove=346, for example. You're spot on, what I need is the URL to appear clean (remove/346) but for $_GET['remove'] to be accessible within the page template. Does that help? Thank you for the response!
    – Gavin
    Oct 6 '10 at 16:40

Since you want to add something to the end of every post/page rewrite rule, you probably can just add a rewrite endpoint. These are regexes of the form /[endpoint_name](/[optional_extra_stuff])? that are appended to the already generated rules for pages, posts, archives, ...

You define on which structures you want to add them by setting the endpoint mask. This is a bitmask, so you can combine different groups using the | operator, like this: EP_PERMALINK | EP_PAGES will match for every page and every permalink (full post and date-based archives). The default list of endpoints can be found at the top of wp-includes/rewrite.php.

The following code will add /remove(/(.*))? to the existing rewrite rules for pages, posts and date-based archives (for some reason they are generated twice, once in EP_PERMALINK and once in EP_DATE). remove will also be available as a public query var, so you can do $wp_query->get( 'remove' ) to get the value (if set) of the [optional_extra_stuff] in the URL.

add_filter( 'init', 'wpse2614_init' );
function wpse2614_init()
    add_rewrite_endpoint( 'remove', EP_PERMALINK | EP_PAGES );

This is totally simple. In you PHP script where you output the ?remove=54 just output remove/54 instead. That should do the job to change the links as you asked for.

  • I think Gavin has no problems changing the links, but "catching" the new request. If you have a WordPress page at /pagetitle, a request for /pagetitle/remove/54 won't give you that without changing the rewrite structure.
    – Jan Fabry
    Nov 9 '10 at 20:49
  • Well, I can not read that exactly in his question. But if so, he could register "remove" as an rewrite endpoint: codex.wordpress.org/Rewrite_API
    – hakre
    Nov 9 '10 at 22:56

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.