2

Links on the page I use are progressing as follows: http://example.com/image/1

The code I use for Functions.php

add_action( 'init', 'wpse316713_pagination_base_rewrite_rule' );
function wpse316713_pagination_base_rewrite_rule() {
    global $wp_rewrite;
    $wp_rewrite->pagination_base = 'image';
}

Reference link: How to change the link structure of the homepage?

What I'm trying to do is remove the "image" base. How can we change it to http://example.com/1?

Note:

 $wp_rewrite->pagination_base = 'image';

If I remove the word "image" here, it's double flip. " // " shaped. This leads to an inability to display the page. Going like http://example.com/image/2/1. How can we completely remove it and return it to only 1?

1 Answer 1

0

To remove pagination prefix page in home page you should:

  • add a rewrite rule which will translate the new link format,
  • prevent redirection to address containing page,
  • replace URLs in paging links.

By creating pagination links, WordPress appends query var paged with mentioned prefix to url. Therefore, without modification, the links would look like example.com/3/page/3.

Next thing is redirection. WP redirects incoming links to the proper URL based on the site url.Thanks to our rewrite rule, for the example.com/3 url, the results of the third page will be displayed (paged=3). But WP check that correct url for paged=3 should be example.com/3/page/3 which is different from example.com/3. Therefore, a redirection will be made.

add_action( 'init', 'se323035_structure', 25 );
add_filter( 'paginate_links',       'se323035_paginate_links' );
add_filter( 'redirect_canonical',   'se323035_redirect_canonical', 20, 2 );

function se323035_structure()
{
    global $wp_rewrite;
    if ( $wp_rewrite->using_permalinks() )
        add_rewrite_rule( '([0-9]+)/?$', 'index.php?&paged=$matches[1]', 'top');
}

function se323035_paginate_links( $link )
{
    global $wp_rewrite;
    if ( is_home() && $wp_rewrite->using_permalinks() ) {
        $original = @parse_url( $link );
        $home = @parse_url( home_url('/') );
        if ( ! isset( $original['path'] ) )
            $original['path'] = '';

        $original_path = $original['path'];
        $original_path_end = $original['path'];
        if ( isset($home['path']) )
        {
            $home['path'] = trim($home['path']);
            $home['path'] = untrailingslashit($home['path']);
            if ( !empty($home['path']) && $home['path'] != '/' )
                $original_path_end = str_replace( $home['path'], '', $original['path']);
        }
        //
        // if original url match to our link structure do not redirect to '/page/{number}'
        if ( preg_match( '|^/?[0-9]+/?$|', $original_path_end) ) {
            $link = home_url( '/' );
        }
        elseif ( preg_match( "|/(\d+/)?$wp_rewrite->pagination_base/\d+/?$|", $original_path_end) )
            $link = preg_replace( "|/(\d+/)?$wp_rewrite->pagination_base/(\d+/?)$|", '/${2}', $link );
    }

    return $link;
}

function se323035_redirect_canonical( $redirect_url, $requested_url )
{
    global $wp_rewrite;

    if ( is_home() && !empty($redirect_url) )
    {
        $original = @parse_url( $requested_url );
        if ( ! isset( $original['path'] ) )
            $original['path'] = '';

        $original_path = $original['path'];
        $home = @parse_url( home_url('/') );
        if ( isset($home['path']) )
        {
            $home['path'] = trim($home['path']);
            $home['path'] = untrailingslashit($home['path']);
            if ( !empty($home['path']) && $home['path'] != '/' )
                $original_path = str_replace( $home['path'], '', $original['path']);
        }
        // if original url match to our link structure don't redirect to '/page/{number}'
        if ( preg_match( '|^/?[0-9]+/?$|', $original_path) )
            return '';
    }
    return $redirect_url;
}

The new link format can not be used if the permalink structure is set to /%post_id%.

5
  • Firstly, thank you. I thought it worked, I'm sorry. (Maybe I'm wrong) But I also want to give information to improve. I've set the link structure to "/% postname%" And I added these codes by deleting the previous code. The caches have been cleared and the connection structure has been updated again. But I couldn't run the code in this case. The page link structure returned to the default settings: (page/1 page/2) I'm trying to figure out where I'm doing wrong.
    – A. Muller
    Commented Apr 18, 2019 at 12:16
  • Did you refresh the permalinks? Click "Save" in Settings -> Permalinks
    – nmr
    Commented Apr 18, 2019 at 12:21
  • Yes. First, I erased the old code. Then I added those codes. Then I went to the permanent link settings. Custom, I've set to "/% postname%". I recorded the changes. I've cleared all the caches. I've also cleared browser caches. But it has returned to the default settings. (Page/1 Page/2) I'm trying to figure out where the mistake is. And I wonder if the code needs to be reviewed again.
    – A. Muller
    Commented Apr 18, 2019 at 12:43
  • Note: Actually, it works as example.com/1 (But when you delete "page" from the link) In other words, the name "Page" in the link structure must be removed. Briefly: The name "Page" must be removed. It works when you delete it from the browser connection. If the "page" link is deleted from the browser, it works in this case. But automatically, the links don't work that way.
    – A. Muller
    Commented Apr 18, 2019 at 12:52
  • Unfortunately. Right now, I can't join this department.
    – A. Muller
    Commented Apr 18, 2019 at 16:27

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.