WordPress – Pass Query string as slash based URL

Picture of Amitpal Singh
Amitpal Singh
January 28, 2022

Are you trying to pass a parameter to a wordpress page. And don’t want to pass it as a query string. You would like to pass as a slash based url? 

Example:

http://localhost/mysite/pagename?user=myname

into

http://localhost/mysite/pagename/myname

Here is the solution:

add_action('init', function(){
   add_rewrite_rule( 
      '^yourPageSlug/([^/]+)([/]?)(.*)', 
      //!IMPORTANT! THIS MUST BE IN SINGLE QUOTES!:
      'index.php?pagename=yourPageSlug&user=$matches[1]', 
      'top'
   );   
}); 

Share this post:

How to Attribute?

Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
for Example: Website, Social Media, Blogs, ebooks , newsletter, etc.
Lorem ipsum is typically a corrupted version of De finibus bonorum et malorum, a 1st-century BC text by the Roman statesman and philosopher Cicero.
Copied!

Got a Question? Check out our FAQ Section.

Your action, our appreciation

It encourage us to give you more valuable content on website.