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

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:

Leave a Comment