Change pagination base slug on WooCommerce archive product page - wordpress

I want to change pagination slug, currently 'page' to 'strana' on WooCommerce archive pages.
Default setting with urls http://localhost/obchod/ and http://localhost/obchod/page/2/ works fine.
When I change pagination base and resave permalinks it not works.
Final url should look like: http://localhost/obchod/strana/2/
This is my code for changing the pagination base:
function custom_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->pagination_base = 'strana';
}
add_action('init', 'custom_rewrite_rules');
I always resave permalinks on WordPress or I use $wp_rewrite->flush_rules();, but non of these works.
Note that this code works for WordPress pagination, but not for WooCommerce pagination shop pages.
What Am I doing wrong?

First we need to change the format of the links that Wordpress generates. As we need to change the pagination format only for the shop archive, we add a conditional tag is_shop() , so another pagination will stay in old format.
function custom_rewrite_rules() {
global $wp_rewrite;
if(is_shop()) {
$wp_rewrite->pagination_base = 'strana';
}
}
add_action('template_redirect', 'custom_rewrite_rules');
Next, we add_rewrite_rule() to make Wordpress understand the new link format for pagination pages
add_rewrite_rule(
'^obchod/strana/([^/]+)/?',
'index.php?paged=$matches[1]',
'top'
);
obchod here = your shop base, default its shop fow woocommerce stores
Update for test
function custom_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->pagination_base = 'strana';
}
add_action('init', 'custom_rewrite_rules');
add_rewrite_rule(
'^obchod/strana/([^/]+)/?',
'index.php?paged=$matches[1]&post_type=product',
'top'
);

Related

How to remove the original link of a post in Wordpress after using post_link and add_rewrite_rule to change the link?

I changed the permalinks of my posts using post_link, adding "/test/" to it.
So for example www.website.com/this-is-fun/ is now having the permalink www.website.com/test/this-is-fun/
This permalink is correctly showing in the admin, when editing a post, and also on the website the link to the post is the new one.
However, when I open this link it is not working, I get a 404. I was able to fix that with a add_rewrite_rule, but now my problem is that BOTH urls exist. I want only the new one to exist.
Am I making a mistake in the post_link part, is there a way you can automatically disable the original link and make the new one work? Or should I use add_rewrite_rule to disable the original? Or is there another way?
This is my code:
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
$permalink = home_url('/test/' . $post->post_name .'/' );
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'test/([^/]+)(?:/([0-9]+))?/?$',
'index.php?name=$matches[1]',
'top'
);
}
add_action( 'init','flush_rewrite_rules' );
I know I can change the permalink in the settings btw, but this new permalink will not be for all posts, so I need to do it this way.

woocommerce my account page showing posts

I'm working on a WordPress plugin where I've to create a custom frontend page. After adding the add_rewrite_tag, my custom frontend page works and all the woocommerce pages (endpoints) works fine but the /my-account page does not.
It shows the posts page. Removing the add_rewrite_tag fixes the /my-account page.
function customop_custom_output() {
add_rewrite_tag( '%pn-projects%', '([^/]+)' );
add_permastruct( 'pn-projects', '/%pn-projects%' );
flush_rewrite_rules();
}
add_action( 'init', 'customop_custom_output' );
Am I missing something here?

Page template for all WooCommerce Files

I have a page template that is a minimalized page (call it min-page.php) in my theme. I use it to remove the header and footer info and add in some basic links so the customer doesn't get distracted.
I can change this template in the Page Attributes on various WooCommerce pages (checkout, cart, my account) but I can't change it on the product page or the general shopping page that displays all the products.
How can I set these pages to use my page template?
You can change the single product template by using the following code via functions.php
function get_product_template($single_template) {
global $post;
if ($post->post_type == 'product') {
$single_template = dirname( __FILE__ ) . '/min-page.php';
}
return $single_template;
}
add_filter( 'single_template', 'get_product_template' );
Check the template path after placing the code.

WordPress custom post type rewrite rule

I'm trying to add a rule for a custom post type. By default, the URL to view post is www.mydomain.com/job/post-slug
What I'd like, is the post also accessible with the following URL:
www.mydomain.com/j/postid
I've tried this in my functions.php file and I also refresh permalinks in admin settings:
function rewrite_short_job_url() {
add_rewrite_rule( '^j/([0-9]+)/?', 'index.php?post_type=job&id=$matches[1]', 'top' );
flush_rewrite_rules( true );
}
add_action( 'init', 'rewrite_short_job_url' );
Doesn't work for me, I'm trying to understand the Rewrite API but cannot find a solution.
I believe you should be using the following:
function rewrite_short_job_url() {
add_rewrite_rule( '^j/([0-9]+)/?', 'index.php?post_type=job&p=$matches[1]', 'top' );
}
add_action( 'init', 'rewrite_short_job_url' );
It's important to keep the post_type var, so that only job posts use that redirect.
You can view a list of WordPress query variables in the Codex.

Wordpress URL rewrite with GET variables

I have tried to search a solution but was unable to find one. In my wordpress website i've got a custom page that retrieves data from the database. Now i have this url:
http://www.domain.com/party/?title=nameoftheparty&id=4
I need to rewrite it to:
http://www.domain.com/party/nameoftheparty/4/
I tried to add the rewrite url in .htaccess but i get an 404 page.
What do i need to do?
Add both the code snippets to functions.php
1.We are telling WordPress that /party/nameoftheparty/4/ should be internally mapped to /party/?title=nameoftheparty&id=4
add_action( 'init', 'so27053217_init' );
function so27053217_init()
{
add_rewrite_rule(
'^party/([^/]*)/([^/]*)/?',
'index.php?pagename=party&title=$matches[1]&id=$matches[2]',
'top' );
}
This is optional and only required if you need to make use of title and id variables in your party page. They can be accessed using get_query_var("title")
add_filter( 'query_vars', 'so27053217_query_vars' );
function so27053217_query_vars( $query_vars )
{
$query_vars[] = 'title';
$query_vars[] = 'id';
return $query_vars;
}
Remember to re-save your permalinks to flush rewrite rules.

Resources