How does WordPress redirect to WooCommerce shop page? [closed]

I’m creating a plugin for WooCommerce, it needs a parameter in the url of shop page like this http://domain/wp/shop/?param=value. It worked fine until I set the shop page as front page, when I access http://domain/wp, it is showing the shop page, but when I access it with the parameter (http://domain/wp/?param=value), it’s showing the blog index page.

So, I want to ask where exactly in the WordPress code that redirects the url to WooCommerce shop page? so I may be able to change the redirection behaviour or something else.

2 Answers
2

You could add a rewrite rule to improve the appearance of the URL while maintaining the same functionality:

As an example:

add_action('init', 'custom_shop_param');

function custom_shop_param() {
    add_rewrite_tag('%param%','([^&]+)');
    add_rewrite_rule('^shop/([^/]+)/?$','index.php?page=shop&param=$matches[1]','top');
}

When you visit http://site/wp/shop/{somevalue} the value that proceeds the /shop/ portion of the URL will be matched and stored in the query var param which is registered throught he use of add_rewrite_tag, the $matches[1] variable holds the value of the regex for the first matched group of your expression, http://site/wp/shop/discountproduct would equate to param=discountproduct for which is accessible via accessing the query_vars as part of the request:

//somewhere in your code....
function parse_shop_request() {
    global $wp_query;
    if ( isset($wp_query->query_vars['param']) ) {
         //do something with $wp_query->query_vars['param']
    }
}

You may also use get_query_var('param') to retrieve query variables.

If http://domain/wp/shop/value clashes or has the potential to clash with products or categories or other pages at that URL depth, then you can extend the rewrite rule a little further:

http://site/wp/shop/get/value

add_action('init', 'custom_shop_param');

function custom_shop_param() {
    add_rewrite_tag('%param%','([^&]+)');
    add_rewrite_rule('^shop/get/([^/]+)/?$','index.php?page=shop&param=$matches[1]','top');
}

Of course, replace /get/ with whatever suits your verbiage or context.

You may even do:

add_action('init', 'custom_shop_param');

function custom_shop_param() {
    add_rewrite_tag('%param%','([^&]+)');
    add_rewrite_rule('^shop/?param=([^/]+)$','index.php?page=shop&param=$matches[1]','top');
}

Helpful links:

  • http://codex.wordpress.org/Function_Reference/get_query_var
  • http://codex.wordpress.org/Rewrite_API
  • http://codex.wordpress.org/Rewrite_API/add_rewrite_tag
  • http://codex.wordpress.org/Rewrite_API/add_rewrite_rule

Leave a Comment