Skip to content

Instantly share code, notes, and snippets.

@lauhakari
Last active December 28, 2021 01:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lauhakari/5089252 to your computer and use it in GitHub Desktop.
Save lauhakari/5089252 to your computer and use it in GitHub Desktop.
Change the pagination url in Wordpress (3+) You can put in a plugin file or your functions.php.
<?php
add_action( 'init', 'my_custom_page_word' );
function my_custom_page_word() {
global $wp_rewrite; // Get the global wordpress rewrite-rules/settings
// Change the base pagination property which sets the wordpress pagination slug.
$wp_rewrite->pagination_base = "new-slug"; //where new-slug is the slug you want to use ;)
}
@snaever
Copy link

snaever commented Mar 28, 2014

Hi,

This doesn't seems to work anymore. When I go to domain.com/new-slug/2 it gives me the 404 page. Do you have an updated version of this?

Thanks.

@lauhakari
Copy link
Author

Hey.
Sorry I haven't answered earlier, haven't seen your question. Can't really remember when or how I used this, prpably should've made a note to myself :P

Anyway, you can check whats in your current rewrite rules if your do this:

global $wp_rewrite;
echo '<pre>';
print_r($wp_rewrite);
echo '</pre>';

Maybe you can find the paginate property there? :)

@stefanoortisi
Copy link

Your code works for the pagination on homepage.
Do you have an idea about how to change the slug even for category pagination?

@maryisdead
Copy link

Works fine with 4.9.4.

And a heads-up: You need to save your permalinks to make this work.

@piotrbox
Copy link

Hey, this works on standard posts, but not on custom post type (gives 404 - als ofter saving permalinks), any way to make ot work for CPT? also is there a way to lose the sug completly (like domain.com/customcat/2 ) now when i live new slug blank i get "//" ( domain.com/postcat//2.

thanx

@morvy
Copy link

morvy commented Jan 15, 2019

Had the same problem, for me the pagination doesn't work for WooCommerce, but after checking all rewrite rules I found out that the permalink structure for WooCommerce is not using pagination_base at all, so find the extra_rules_top rule that has "page" in it and reuse the rewrite rule like this:

unset($wp_rewrite->extra_rules_top["eshop/page/([0-9]{1,})/?$"]);
$wp_rewrite->extra_rules_top['eshop/new-slug/([0-9]{1,})/?$'] = 'index.php?post_type=product&paged=$matches[1]';

@obsidianlycan
Copy link

Ah is there anyway to strip the / before the page number? I am trying to re-write to /?=page_2/ and this method drops a / before the 2.

@Spepe13
Copy link

Spepe13 commented Nov 19, 2020

Well it works fine!!!

@saladgoat
Copy link

This is great, I am using on my home page with the slug home/page but have only just realized that it doesn't work on other pages with pagination. End up with a 404.

I tried adding if is_home but that broke the function entirely. Probably because that if statement doesn't work from the functions.php? I don't know....

Is there any way to have this affect only the home page?

Thanks!

@lauhakari
Copy link
Author

@saladgoat Yeah, not sure you can (easily) have different pagination_base's for different sections.
I believe that pagination_base is more used to change the base path on the whole site. :)

@saladgoat
Copy link

@lauhakari I was afraid of that, but trying to be optimistic, thought I'd ask people who know way more about this than me....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment