I'm developing a plugin, that should display content according to two optional parameters in the URL. The following URL should all be treated by the plugin:


Now I have put in place these things:

  • A page with a shortcode [myplugin] and the URL /pluginpage/
    • The shortcode that displays the content taking the parameters from the $_GET variable
    • The plugin works if the URL looks like /pluginpage/?param1=value1&param2=value2
  • Two rewrite rules in place, that match the three URL patterns to the page with the shortcode
add_rewrite_rule('^(pluginpage)/(.*)/?', 'index.php?name=$matches[1]&param1=$matches[2]', 'top');
add_rewrite_rule('^(pluginpage)/(.*)/(.*)/?', 'index.php?name=$matches[1]&param1=$matches[2]&param2=$matches[3]', 'top');

The rewrite rules work, but not as expected. When navigating to /pluginpage/value1/ or /pluginpage/value1/value2/ the page with the shortcode is displayed, but there is a redirect to /pluginpage/ and the parameters are lost during the redirect.

There is a missing piece that tells wordpress to not redirect from /pluginpage/value1/ to /pluginpage/. What exactly is missing?

Or might there be a better approach by mapping the URLs directly to the plugin without using a page and a shortcode?

This question is quite similar, but the answers are lacking important details, and therefore were not helpful in my case.

Thank you for any help.


1 Answer 1


Your code seems to work fine with slight modifications. Taken from here: https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-content-4787

First off, I moved the rewrite rules to init and modified the regex to match parameters optionally:

function wse_414800_rewrite_state_city() {


add_action('init', 'wse_414800_rewrite_state_city');

And, to be able to retrieve our custom query params I've added a function to register custom tags.

// Register query parameters for state and city
function wse_414800_register_query_param() {
    add_rewrite_tag('%state%', '([^&]+)');
    add_rewrite_tag('%city%', '([^&]+)');
add_action('init', 'wse_414800_register_query_param' );

And later on, you can retrieve the parameter either in your shortcode or in a page template using get_query_var()

For your query: "Or might there be a better approach by mapping the URLs directly to the plugin without using a page and a shortcode"

You can use a Page template as an alternative, here is an example for registering and using page template: https://developer.wordpress.org/reference/functions/add_rewrite_rule/#comment-content-4554)

Further it depends on what type of plugin you're building, if it's for internal use you can choose either of the options shortcode or template, but if you plan to publish the plugin IMHO a Gutenberg block or shortcode would do better.

function wse_414800_show_state_city_cb( $atts ) {

    $state = get_query_var('state');
    $city = get_query_var('city');

    return "State: " . esc_html( $state ) .", City: " .esc_html( $city );

add_shortcode( 'show_state_city', 'wse_414800_show_state_city_cb' );

You could utilize the plugin like Query Monitor to check what/how the rewrite rule is working.

Query Parameter - Rewrite rule match

  • Thanks a lot. I will apply your recommendations, and hopefully get it work. Commented Mar 27, 2023 at 12:03
  • I'm still getting the redirection from /pluginpage/value1/ or /pluginpage/value1/value2/ to /pluginpage/, and I don't find any difference between your solution and mine that could explain it. Commented Mar 27, 2023 at 13:13
  • @LorenzMeyer Did you try saving permalinks after adding the code? Because the code definitely works fine on my local test server.
    – Kumar
    Commented Mar 28, 2023 at 5:51
  • 1
    @LorenzMeyer the main difference between @Kumar's solution and yours, is that he used the query var pagename, whereas yours used name, which would work, if you actually have a regular post with the slug pluginpage. If you didn't have that post, but you had a Page (post type page), then that explains why the redirect happened, i.e. you used the wrong query var in your rewrite rule. So as Kumar said, try flushing the rewrite rules by simply visiting the Permalink Settings admin page.
    – Sally CJ
    Commented Mar 29, 2023 at 17:16
  • 1
    Thank you for the explanation @SallyCJ
    – Kumar
    Commented Mar 30, 2023 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.