woocommerce change product permalink to products - wordpress

I have at the moment url in product view: https://mywebsite.com/product/product-name
Shop url looks like this: https://mywebsite.com/shop
I need to change product view url like https://mywebsite.com/products/product-name
I didnt find any settings for that.

I tried this, It is working fine for me
let me know your thoughts..
function change_post_types_slug( $args, $post_type ) {
if ( 'product' === $post_type ) {
$args['rewrite']['slug'] = 'products';
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );

Permalink structure like the following screenshot will give the intended result.
Link to image: https://snipboard.io/xGJWK6.jpg

Related

Add Rewrite Rule for Single Custom Post Type

I'm having trouble creating a custom URL for certain single post types. whereas it works for posts. Here is my code to change the URL of only certain posts:
/**
* Rewire the permalink of individual posts so that they direct to the Free Cakes section
*/
add_filter( 'post_link', 'ae_replace_freecakes_posts_link', 90, 2 );
function ae_replace_freecakes_posts_link( $permalink, $post ) {
global $freecakes_blog_page_id;
if ( ae_get_post_acces_level( $post->ID ) >= 110 ) {
return get_permalink( $freecakes_blog_page_id ) . "$post->ID/{$post->post_name}/";
}
return $permalink;
}
/**
* Rewire the permalink of individual videos so that they direct to the Free Cakes section
*/
add_filter( 'post_type_link', 'ae_replace_freecakes_videos_link', 90, 2 );
function ae_replace_freecakes_videos_link( $permalink, $post_object ) {
global $freecakes_video_page_id;
if ( $post_object->post_type != 'video' ) {
return $permalink;
}
if ( ae_get_post_acces_level( $post_object->ID ) >= 110 ) {
return get_permalink( $freecakes_video_page_id ) . "$post_object->ID/{$post_object->post_name}/";
}
return $permalink;
}
And later this:
add_action( 'init', 'ae_add_rewrite_rules' );
function ae_add_rewrite_rules() {
add_rewrite_rule( "^freecakes/video/([0-9]+)/([^/]*)/?$", 'index.php?p=$matches[1]', 'top' );
add_rewrite_rule( "^freecakes/posts/([0-9]+)/([^/]*)/?$", 'index.php?p=$matches[1]', 'top' );
}
The problem is that this doesn't work for videos. If I use this fictional exemplary url it works just fine:
https://mywebsite.com/freecakes/posts/3892/my-first-frecake-post/
But this URL (the one matched to the video custom post type) gives a 404 error:
https://mywebsite.com/freecakes/posts/10109/my-first-frecake-video/
Any clues of what might be my mistake here? Thank you!
Did you flush and regenerate the rewrite rules database after modifying rules. From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.
As it turns out, even though calling https://mywebsite.com/index.php?p=10109 would normally work while using the navigation bar, it doesn't work for custom post types when using add_rewrite_rule
The rule that works is:
add_rewrite_rule( "^freecakes/posts/([0-9]+)/([^/]*)/?$", 'index.php?video=$matches[2]', 'top' );
Where $matches[2] is the slug of the custom post type (in this case a post_type called "video")

URL to product page with quantity inputed - WooCommerce

I am trying to find a way to edit a product url in WooCommerce so that when visited a quantity is pre-selected for the quantity input, e.g. 2.
I know there is an add to cart url for it:
http://yourdomain.com/?add-to-cart=47&quantity=2
But was wondering if it was possible to just input the quantity on the product page with a similar URL.
Thanks for any ideas on the matter.
you can do it like this:
add_filter( 'woocommerce_quantity_input_args', 'custom_woocommerce_quantity_input_args' ); // Simple products
add_filter( 'woocommerce_available_variation', 'custom_woocommerce_quantity_input_args' ); // Variations
function custom_woocommerce_quantity_input_args( $args ) {
if ( isset( $_GET['qty'] ) && is_numeric($_GET['qty']) ) {
$args['input_value'] = $_GET['qty'];
}
return $args;
}
with that you can then do http://yourdomain.com/product/ship-your-idea/?qty=10.

Remove category base from WordPress url only for specific category

I would like to remove the category base from Wordpress URL only for specific category.
For example, i need to change:
mysite.com/category/blog
to
mysite.com/blog
but i want to keep other categories unchanged:
mysite.com/category/songs
I think that it could be achieved with some .htaccess rule, but I found some generic rules that remove the basic category in all the url.
you can easily achieve this by using Enhanced Custom Permalinks Wp plugin. you just need to go edit the category, yo will see a field to add your custom url.
https://wordpress.org/plugins/enhanced-custom-permalinks/
This can be accomplished with some custom filters & actions.
Try placing this code in your theme's functions.php file:
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the categories for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "News" ) {
$permalink = trailingslashit( home_url('/'. $post->post_name .'-'. $post->ID .'/' ) );
}
return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
// This rule will will match the post id in %postname%-%post_id% struture
$new_rules['^([^/]*)-([0-9]+)/?'] = 'index.php?p=$matches[2]';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite;
}
This will set up the permalink structure that you want for posts:
You can easily do that without using plugins.
Through Admin panel
go to settings->permalinks and select default to custom
here you know more..
https://codex.wordpress.org/Using_Permalinks

Getting WordPress to automatically show category permalink for only specific categories

I have searched, here is the closes result.
I am building a new wordpress site. I want most posts to have no category in the URL, simply www.site.com/title. However I do want the blog posts to be separate, so I'd like www.site.com/blog/title. I'd also like the option to add more like that in the future, for only specific categories, not the entire site.
There are many questions similar to this here on stackoverflow but most have 0 replies. Any advice would be great. I've even tried Advanced Permalinks without any luck.
You can simply do that by Setting > Permalinks and add to Common Setting > Custom Structure the value /blog/%postname%/. There you will get the blog post accessible from www.site.com/blog/title.
I cannot understand the first question. By:
I want most posts to have no category in the URL
do you mean to NOT HAVING www.site.com/category/category-name? or not having www.site.com/category/post?
EDIT #1
To answer this:
www.site.com/category/post is what I ONLY want for blog posts with the category of "Blog" > if the category is "Shoes" I don't want the category displayed in the URL. –
First: you can set the Permalink to /%postname%/ so all your post will have site/title therefore accessible from that link
Second: You have to filter the permalink to behave differently for the posts under "Blog" category.
Try this
add_filter( 'post_link', 'custom_permalink', 10, 2 );
function custom_permalink( $permalink, $post ) {
// Get the categories for the post
$categories = wp_get_post_categories( $post->ID );
foreach ( $categories as $cat ) {
$post_cat = get_category( $cat );
$post_cats[] = $post_cat->slug;
}
// Check if the post have 'blog' category
// Assuming that your 'Blog' category slug is 'blog'
// Change 'blog' to match yours
if ( in_array( 'blog',$post_cats ) ) {
$permalink = trailingslashit( home_url( 'blog/' . $post->post_name ) );
}
return $permalink;
}
Third: You have to filter rewrite_rules
add_filter( 'rewrite_rules_array', 'custom_rewrite_rule' );
function custom_rewrite_rule( $rules ) {
$new_rules = array(
'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]'
);
$rules = $new_rules + $rules;
return $rules;
}
Go to permalink setting and save the setting to refresh your rewrite rules and make the changes above active
NOTE: Add those functions on your active theme functions.php template
NOTICE: I haven't test it yet but that's the way you change permalink. I did the similar way to change my permalink on archives and search result.

Changing permalink to custom post type

I am trying to get my permalink to be something like this:
http://example.com/projects/1234
By default it is looking like this:
http://example.com/projects/title
I tried setting the "slug" to "projects/%post_id%" but then it looked like this:
http://example.com/projects/%post_id%/title
Is there a way to set the slug to my custom slug "/" the id for the post? Any help is appreciated.
A gift from me to you: Just replace "property" with the name of your custom post type. Goes in your theme's functions.php
add_filter('post_type_link', 'property_post_type_link', 1, 3);
function property_post_type_link( $link, $post = 0 ){
if ( $post->post_type == 'property' ){
return home_url( 'property/' . $post->ID );
} else {
return $link;
}
}
add_action( 'init', 'property_rewrites_init' );
function property_rewrites_init(){
add_rewrite_rule(
'property/([0-9]+)?$',
'index.php?post_type=property&p=$matches[1]',
'top' );
}
Is 'projects' a category or tag etc?
Something like the below should work if projects is a category or tag.
/%category%/%post_id%/

Resources