17

Simple question. When pagination is activated, the URL changes to "site.com/page/2". For my site, this should be "site.com/paggetto/2".

How can I change that rewrite rule? I also want to change "author" and other variables.

4 Answers 4

20

For some sites in German I use the following plugin to translate page to seite (the German word for page):

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: T5 Page to Seite
 * Description: Ersetzt <code>/page/</code> durch <code>/seite/</code>.
 * Author:      Fuxia Scholz
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 */

if ( ! function_exists( 't5_page_to_seite' ) )
{
    register_activation_hook(   __FILE__ , 't5_flush_rewrite_on_init' );
    register_deactivation_hook( __FILE__ , 't5_flush_rewrite_on_init' );
    add_action( 'init', 't5_page_to_seite' );

    function t5_page_to_seite()
    {
        $GLOBALS['wp_rewrite']->pagination_base = 'seite';
    }

    function t5_flush_rewrite_on_init()
    {
        add_action( 'init', 'flush_rewrite_rules', 11 );
    }
}

Note that you flush the rewrite rules on de/activation only. You will need a separate rewrite rule in your .htaccess to redirect old URLs to the new ones:

RedirectMatch Permanent ^/(.*)/page/(.*) /$1/seite/$2
2
  • 1
    What's the difference between $GLOBALS['wp_rewrite']->pagination_base = 'seite' and $wp_rewrite->search_base = 'buscar'; ? Commented Jul 1, 2012 at 21:05
  • 1
    There are subtle and unintuitive differences between global $var; and $GLOBALS['var']. Doesn’t matter in this case, just keep in mind that the latter form is more reliable and easier to read/understand.
    – fuxia
    Commented Jul 1, 2012 at 21:09
23

Figured out:

function re_rewrite_rules() {
    global $wp_rewrite;
    // $wp_rewrite->author_base = $author_slug;
//  print_r($wp_rewrite);
    $wp_rewrite->author_base        = 'autor';
    $wp_rewrite->search_base        = 'buscar';
    $wp_rewrite->comments_base      = 'comentarios';
    $wp_rewrite->pagination_base    = 'pagina';
    $wp_rewrite->flush_rules();
}
add_action('init', 're_rewrite_rules');

At least, that will do the job.

1
  • 18
    Just one small notice, because of how extremely costly $wp_rewrite->flush_rules(); is in terms of performance, you should never use it on init, a much better option is to just visit the Permalink options page, and save the changes a few times, that will flush them for you.
    – Andre A
    Commented Jul 24, 2012 at 11:50
1

This function will work directly with your translation package, formatting your new base and prevent to run more than once the flush_rewrite_rules function avoiding bad performance of your blog.

function my_change_rewrite_base() {
    global $wp_rewrite;
    $bases = array(
        'author_base' => __('Author'), 
        'search_base' => __('Search'), 
        'comments_base' => __('Comments'), 
        'pagination_base' => __('Page')
    );

    foreach ($bases AS $key => $base) {
        $wp_rewrite->{$key} = remove_accents(mb_strtolower($base));
    }

    if ( ! get_option('my_change_rewrite_base_flushed', false) ) {
        flush_rewrite_rules();
        update_option( 'my_change_rewrite_base_flushed', time());
    }
}
add_action('init', 'my_change_rewrite_base');
0

The following worked for me:

function nw_strana() {
    $GLOBALS['wp_rewrite']->pagination_base = 'strana';
}

add_action( 'init', 'nw_strana' );

function nw_rewrite( $rules ) {
    $new_rules = array(
        'obchod/strana/([0-9]{1,})/?$' => 'index.php?post_type=product&paged=$matches[1]',
    );

    $rules = array_merge( $new_rules, $rules );

    return $rules;
}

add_filter( 'rewrite_rules_array', 'nw_rewrite' );
3
  • Any idea to remove the "page" from the slug for pagination? Default mydomain/page/2. I want it to be mydomain/1 Thanks in advance!
    – Jornes
    Commented Jan 7, 2023 at 15:19
  • I wouldn't recommend doing that @Jornes, too much confusion with posts/pages which also don't use a (slug) prefix.
    – Beee
    Commented Mar 29, 2023 at 10:47
  • @Beee Yes. That makes sense. Now, I just leave it as it is.
    – Jornes
    Commented Jun 27, 2023 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.