Change the “page” slug in pagination

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.

Related posts

Leave a Reply

4 comments

  1. 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. 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.

  3. 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');
    
  4. 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' );