Search code examples
wordpresspermalinks

How to add several parameters with slash in wordpress post url?


I want to add several parameters with slash not by question mark in Wordpress Post URL. Thanks in advance.


Solution

  • You can achieve by WordPress Standard. See below quick step and follow WordPress docx for specific functions/filters, if you get stuck.

    You need to use add_rewrite_rule as per WordPress Standard to pass query string as a slash based URL

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

    This filter is used to target the query string variable.

    
        add_filter('query_vars', function( $vars ){
            $vars[] = 'pagename'; 
            $vars[] = 'page_id'; 
            return $vars;
        });
    
    

    Update permalink settings

    This will be used to get the value in a specific page

    get_query_var( 'page_id' )