I've been trying to solve this for two days now, and I still don't understand what I'm doing wrong.
So, the problem is very simple. I have a simple page with the URL mywebsitedomain/shop-the-look/. I'm using a custom page for it, which gets the variable named "outfit" value and then extracts the specific data from the backend.
mywebsitedomain/shop-the-look/?outfit=blue-suit-1 mywebsitedomain/shop-the-look/?outfit=white-tshirt-2 etc.
I get this value and then show the appropriate outfit; there are no problems here. What I want to achieve is to have the URL like this:
mywebsitedomain/shop-the-look/blue-suit-1/
I dug through some posts on StackExchange and found out that I need to do something like this:
function add_custom_rewrite_rules() {
add_rewrite_rule('^shop-the-look/([^/]+)/?$', 'index.php?page_id=22394&outfit=$matches[1]', 'top');
add_rewrite_tag('%outfit%', '([^&]+)');
}
add_action('init','add_custom_rewrite_rules');
But it does not seem to work. Now, when I enter
mywebsitedomain/shop-the-look/blue-suit-1/
it just redirects me to
mywebsitedomain/shop-the-look/
I don't know what I'm doing wrong here. I'm not a professional developer, so I might have missed something, so I would really appreciate someone's help.
I've been trying to solve this for two days now, and I still don't understand what I'm doing wrong.
So, the problem is very simple. I have a simple page with the URL mywebsitedomain/shop-the-look/. I'm using a custom page for it, which gets the variable named "outfit" value and then extracts the specific data from the backend.
mywebsitedomain/shop-the-look/?outfit=blue-suit-1 mywebsitedomain/shop-the-look/?outfit=white-tshirt-2 etc.
I get this value and then show the appropriate outfit; there are no problems here. What I want to achieve is to have the URL like this:
mywebsitedomain/shop-the-look/blue-suit-1/
I dug through some posts on StackExchange and found out that I need to do something like this:
function add_custom_rewrite_rules() {
add_rewrite_rule('^shop-the-look/([^/]+)/?$', 'index.php?page_id=22394&outfit=$matches[1]', 'top');
add_rewrite_tag('%outfit%', '([^&]+)');
}
add_action('init','add_custom_rewrite_rules');
But it does not seem to work. Now, when I enter
mywebsitedomain/shop-the-look/blue-suit-1/
it just redirects me to
mywebsitedomain/shop-the-look/
I don't know what I'm doing wrong here. I'm not a professional developer, so I might have missed something, so I would really appreciate someone's help.
You're very close! This is a common WordPress hurdle, and the issue is usually due to one or more of the following:
Your code is almost correct, but let's make sure it's robust:
function add_custom_rewrite_rules() {
add_rewrite_rule(
'^shop-the-look/([^/]+)/?$',
'index.php?pagename=shop-the-look&outfit=$matches[1]',
'top'
);
add_rewrite_tag('%outfit%', '([^&]+)');
}
add_action('init', 'add_custom_rewrite_rules');
shop-the-look
, use pagename=shop-the-look
.
Whenever you add or change rewrite rules, you must flush them. The easiest way is to visit Settings → Permalinks in your WordPress dashboard and click "Save Changes" (no need to actually change anything).
Alternatively, you can add flush_rewrite_rules();
temporarily in your code, but remove it after use to avoid performance issues.
In your shop-the-look
page template, retrieve the variable like this:
$outfit = get_query_var('outfit');
if ($outfit) {
// Fetch and display the outfit data
echo 'Outfit: ' . esc_html($outfit);
} else {
// Show default or error message
echo 'No outfit selected.';
}
shop-the-look
page exists and is published.Here's a full snippet for your functions.php
:
function add_custom_rewrite_rules() {
add_rewrite_rule(
'^shop-the-look/([^/]+)/?$',
'index.php?pagename=shop-the-look&outfit=$matches[1]',
'top'
);
add_rewrite_tag('%outfit%', '([^&]+)');
}
add_action('init', 'add_custom_rewrite_rules');
Flush permalinks after saving this!
functions.php
.get_query_var('outfit')
in your template.If it still doesn't work:
shop-the-look
.index.php?page_id=22394&outfit=$matches[1]
if you prefer using the page ID.functions.php
.Let me know if you need more help or if you get any errors!
page_id=22384
to something like?pagename=shop-the-look
otherwise the entire thing breaks if someone accidentally deletes theshop-the-look
page, even if it gets recreated due to the ID change. I also question the need foradd_rewrite_tag
, and I'd be wary that this won't set a$_GET['outfit']
value either, you'll need to useget_query_var
and update the query vars to includeoutfit
as a valid parameter – Tom J Nowell ♦ Commented May 10 at 14:28