Custom rewrite rules on Wordpress - wordpress

I have the following url query that is adding the parameter texas and the county harris to the permalink for a query. These are part of a custom taxonomy called geography.
xyz.com/dealers/honda/?geography=texas&geography=harris
How do I rewrite this url in wordpress to be:
xyz.com/dealers/honda/texas/harris/

you can do the following in your functions.php:
add_rewrite_rule('^dealers/honda/([^/]*)/([^/]*)/?','index.php?geography=$matches[1]&geography=$matches[2]','top');
However you should note that you have geography set twice and the last one is the one that will be used.
You will also need to navigate to your permalinks page at xyz.com/wp-admin/options-permalink.php before this actually works or else you'll get a 404ish error.

Related

Wordpress url parameter "country" on every page

I would like to use a custom URL parameter for my wordpress site, called country.
This parameter should only be used internally.
For translation I use WPML. I know that it would be possible to create a language for every country / language combination but then I would have to maintain the same language for so many countries and that would be too much effort.
Anybody got an idea how I can make the url look something like that?
www.my-domain.com/de/at -> front page
www.my-domain.com/de/at/my-post -> other page
add_action('init', 'rewritecustom');
function rewritecustom(){
add_rewrite_tag('%country%', '([^&]+)', 'country=');
add_rewrite_tag('%lang%', '([^&]+)', 'lang=');
add_rewrite_rule(
'^([a-z]{2})\/([a-z]{2})\/([^\/]*)',
'index.php?pagename=$matches[3]&country=$matches[2]&lang=$matches[1]',
'top'
);
}
In the permalink settings I set the url to
https://www.my-domain.com/%lang%/%country%/%postname%/
But unfortunately I get a 404 error when I try this.
Anybody got an idea how to solve it?

WordPress post permalinks for multiple categories, tags and taxonomy terms

I'm creating a WordPress plugin and one of the requirements is that a custom post type (photo) with a custom taxonomy (photos) needs to generate multiple permalinks if the post is assigned to multiple terms... so for example if a post is assigned to two taxonomy terms ("birthday" and "wedding") both of the following permalinks should display the post:
/photos/bithday/photo/post-name
/photos/wedding/photo/post-name
I currently have the following rewrite rule defined, which is returning a 404:
add_rewrite_rule( 'photos/(.*)/photo/(.*)?', 'index.php?post_type=photo&taxonomy=photos&term=$matches[1]&pagename=$matches[2]', 'top' );
However, if I visit the following URL which matches the pattern of my rewrite rule:
/index.php?post_type=photo&taxonomy=photos&term=birthday&pagename=post-name
It returns the correct content, but only after redirecting to this URL:
/photo/post-name/?taxonomy=photos&term=birthday-cakes
I must be missing something, any help would really be appreciated!
I managed to get this working by changing &pagename= to &name=.

Wordpress custom post type in wrong taxonomies

I've add a custom post type with custom taxonomies. I've also added rewrite rules in order to handle following urls:
courses/languages/english/english-course
where courses is the base slug, languages and english are taxonomy slugs and english-course is my custom post. english-course is a child taxonomy of languages and english-course has been categorized in english-course.
My rewrite rule is:
courses/(.+)/(.+)/(.+)/?$ => index.php?thr_course=$matches[3]
Everything works fine but any categories are permitted using this syntax. All following urls are legitimate and work as well:
courses/languages/french/english-course
course/products/english/english-course
course/anycategory/anycategory2/english-course
My rewrite rule is pretty obvious: it only matches my post name ignoring which categories it belongs to.
Where and how should I implement a check in order to return 404
if post exists but parent taxonomies are wrong?
I think WordPrss will not give you a solution for your customization automatically. Fetch other parameters from like parent / child category from URL, similar to thr_course parameter. Use this parameter in your query to narrow down your result.
Hope this will help you.

Appending query parameter to Woocommerce category URL

I'm trying to force display the list view in my Woocommerce store but I can't seem to get it working.
The theme has support for list view and you can force it by appending "?product_view=list" so a category URL becomes:
http://subliminalscience.com/product-category/icbch-hypnosis-certification/?product_view=list
Instead of the default one:
http://subliminalscience.com/product-category/icbch-hypnosis-certification/
I added this Rewrite Rule to my htaccess but it doesnt:
RewriteRule ^product-category(.*)$ http://subliminalscience.com/product-category$1?product_view=list
It seems Wordpress ignores this Rewrite rule. Any ideas?
I'm surprised this answer stands as the only one.
Making changes in .htaccess to force this sort of behaviour seems really unnecessary and obviously isn't useful for others trying to solve this issue, especially those users on nginx servers or on shared hosting without .htaccess access.
You should try to fix this in PHP using an action.
Looking at your question, I can tell that theme you have is checking for GET data in the URL bar, which is the ?key1=value1&key2=value2 part of an URL. GET is an HTTP method that you can read about here if you want to learn more.
You can actually set GET data in PHP, and you can safely put this into your functions.php file.
You will want to create a function that simply checks the current page, and if it's a product category page sets the GET data.
At the bottom of your functions.php, you'd want to add something like this:
<?php
//Force all category pages to list view.
add_action('woocommerce_before_main_content','force_category_list_view', 5);
function force_category_list_view(){
if(is_product_category()){
$_GET['product_view'] = 'list';
}
};
?>
I actually can't test this, but I think it'd work. Essentially, in the woocommerce template archive-product.php, the first action that runs is 'woocommerce_before_main_content'. What we're doing is calling our function, which checks the page is indeed a product category. If it is, it sets the GET variable as list, which is exactly what the URL is telling the page to do already.
Mainly this is just a better practise than altering your .htaccess, but someone determined could also override that GET data by changing the URL to read ?product_view=list&product_view=xyz I can't imagine this would be an issue in this instance, but in other instances it might be.

Rewrite category wordpress

Is it possible to rewrite only one category ?
I have a category "Photos" and just for this category.
I just want to rewrite it from /category/photos to /photos
You can do it with WP Rewrite API.
Add this to functions.php
add_action('init', 'register_rewrites');
function register_rewrites() {
add_rewrite_rule('^photos$', 'index.php?category_name=photos','top');
}
Remember to reload your rewrite settings: go to Settings -> Permalinks and click on Save button - no changes needed.
The Yoast SEO plugin has this function build in, I recommend this plugin in general.
no-category-base-wpml only does this
If you don't want to install a plugin for this. Around the web there are many tutorials which do this:
http://thisismyurl.com/6704/remove-category-url-wordpress/
http://www.webdevtuts.net/php/how-to-remove-category-from-wordpress-url-structure/
http://www.wprecipes.com/how-to-remove-category-from-your-wordpress-url
Too enable this for just one category I would advise the following:
create a page called photos.
create a theme template page for this photo page
In is set up a new query_posts* getting the category 'photos'
Include the category.php file.
assign that template to the photo page.
Not tested but should work. Because the page has the correct URL and should include the category. Questions, aks.
*query_posts is bad for performance if you want to do it totally correct use the pre_get_posts filter. It's requires more knowledge.

Resources