A specific post on my website has a shortcode that uses a parameter called "query" to produce its content. So, this is an example of a working URL:

https://example.com/my-post?query=980

"my-post" is the permalink of the post and the page is using the "query" param correctly.

However, we need to change this URL to a friendly one like https://example.com/something-here/980 and then I was trying to use the add_rewrite_rule like this:

function custom_add_rewrite_rules() {
    add_rewrite_rule(
        '^something-here/([\d]+)$',
        'index.php?p=10219&query=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_add_rewrite_rules');

When I try to access the friendly URL, I'm redirected to https://example.com/my-post successfully, but the query param is null ($_GET["query"] = null).

I also tried making use of add_rewrite_tag to register "query", but with no success.

If anyone has some light to shed it'd be more than appreciated.

up vote 1 down vote accepted

It sounds like the plugin expects there to be a get param.

Your rewrite-rule is removing the get-param before it can be used by the plugin.

If the plugin hasn't been updated for a long time (it's abandonware) then you can go through the plugin and replace the get-param with the portion of the url which would have been the get-param. Just make sure to prevent the plugin from updating after you've made your edits.

If you add the following

function custom_add_rewrite_rules() {
  add_rewrite_tag('%query%','([^&]+)');

  add_rewrite_rule(
      '^something-here/([\d]+)$',
      'index.php?p=10219&query=$matches[1]',
      'top'
  );
}
add_action('init', 'custom_add_rewrite_rules');

& then replace $_GET['query'] &/or $_REQUEST['query] with get_query_var( 'query' ) in the plugin you've described, that should be enough to do the trick.

Stephen Harris sums it up nicely in the answer here

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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