Skip to:
Content
Pages
Categories
Search
Top
Bottom

Problem next_posts_link() in P2 integration attempt

  • Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    I’ve integrated the P2 theme into my custom theme. I have an annoying problem with the next_posts_link() and previous_posts_links() tags. They produce links like these:

    mysite.com/blogname/blogname/page/2/

    That would work fine if the blogname wasn’t put in there twice. P2 uses its own (?) function posts_nav_link, instead of next_posts_link, but the result is the same.

    The links are formed by a function get_pagenum_link() in wp-includes/link-template.php. The magic happens here somewhere:

    ...
    if ( $pagenum > 1 ) {
    $result = add_query_arg( 'paged', $pagenum, $base . $request );
    } else {
    $result = $base . $request;
    }
    } else {
    $qs_regex = '|?.*?$|';
    preg_match( $qs_regex, $request, $qs_match );

    if ( !empty( $qs_match[0] ) ) {
    $query_string = $qs_match[0];
    $request = preg_replace( $qs_regex, '', $request );
    } else {
    $query_string = '';
    }

    $request = preg_replace( '|page/d+/?$|', '', $request);
    $request = preg_replace( '|^index.php|', '', $request);
    $request = ltrim($request, '/');

    $base = trailingslashit( get_bloginfo( 'url' ) );

    if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
    $base .= 'index.php/';

    if ( $pagenum > 1 ) {
    $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
    }

    $result = $base . $request . $query_string;
    }

    $result = apply_filters('get_pagenum_link', $result);
    ...

    I’ve tried editing the preg_replace regex mess, with some partial results, but I have no clue what’s going on exactly in this function.

    Can anyone spot what is going on here? Why does the blogname show up twice? What can I remove to remove one?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Avatar of jivany
    jivany
    Participant

    @jivany

    The first thing I’d do to start debugging is to find out what the value of $base and $request are where it gets assigned to $result. If you see blogname in both, that’s your problem. ;)

    $base = trailingslashit( get_bloginfo( 'url' ) );

    The above line should dump whatever you have set in you WP General Settings as the Blog URL. If that includes blogname, then the code creating the $request is doing something funky.

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    Thanks jivany!

    When I replace that $base with ‘http://thewebsite.com/’ there’s only one blogname. Unfortunately then the link that’s supposed to go back to the blog index goes to the main site homepage.

    So $request adds the blogname again? I think that’s what led me to the $request = preg_replace regex stuff. That’s where I get stuck…

    Avatar of jivany
    jivany
    Participant

    @jivany

    So where does $request come from? It appears to be assigned before the code snippet you have provided.

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    In that function get_pagenum_link() the first appearance is this line:

    $request = remove_query_arg( 'paged' );

    Avatar of jivany
    jivany
    Participant

    @jivany

    Oh, looking at the code now…

    Do you have your Blog URL and WP URL (On the WP settings page) the same?

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    Which WP settings page?

    Avatar of jivany
    jivany
    Participant

    @jivany

    Jebus. That is one F’ed up function. It uses three different ways to get what should be the same data.

    $home_root = parse_url(get_option('home'));

    $base = trailingslashit( get_bloginfo( 'home' ) );

    $base = trailingslashit( get_bloginfo( 'url' ) );

    These *should* all return the same value and I can’t see anywhere else that the code would potentially break. The only thing I can suggest is to dump those three values above and make sure they all match.

    The only other thing I see is to ensure that you aren’t using the default permalink structure. I think you have to change this to use BP though so that shouldn’t be your problem.

    Avatar of jivany
    jivany
    Participant

    @jivany

    Which WP settings page?

    The WordPress General settings page

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    I don’t see any Blog URL or WP URL settings in wp-admin/options-general.php or wpmu-options.php

    But the problem sees to be in that $request part. $base pretty much does what it is supposed to do, despite the confusing different ‘tags’. The P2 theme is not for WPMU/BP, so it’s not really a surprise things get mixed up on member blogs.

    Thanks for the suggestions, jivany! I have to drop this now and get back to my day job…

    Avatar of jivany
    jivany
    Participant

    @jivany

    Ahh. WPMU is likely different. Sorry, I keep forgetting that most people are using WPMU with BP. I have to stop assuming. ;)

    You might want to ask on a WPMU specific site because if that function is using three different ways to get the same value on single WP, it might actually be doing something weird on WPMU in one of those calls (two of which are considered deprecated).

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    This works to remove doubles from a URL:

    $string = "http://website.com/blogname/blogname/page/2/";
    $string = preg_replace("/([,.?!])/"," \\1",$string);
    $parts = explode("/",$string);
    $unique = array_unique($parts);
    $unique = implode("/",$unique);
    $unique = preg_replace("/\s([,.?!])/","\\1",$unique);
    echo $unique;

    This outputs:

    http://website.com/blogname/page/2

    :-)

    Now I have to turn it into a function…

    Avatar of Peterverkooijen
    peterverkooijen
    Participant

    @peterverkooijen

    OK, slightly modified to prevent the function from messing with dots (.) and return the result instead of echo:

    function removedoubles($string){
    $string = preg_replace("/([,?!])/"," 1",$string);
    $parts = explode("/",$string);
    $unique = array_unique($parts);
    $unique = implode("/",$unique);
    $unique = preg_replace("/s([,?!])/","1",$unique);
    return $unique;
    }

    This applied to function next_posts and previous_posts in /wp_includes/link_template.php:

    function next_posts( $max_page = 0, $echo = true ) {
    $output = removedoubles(esc_url( get_next_posts_page_link( $max_page ) ) );

    if ( $echo )
    echo $output;
    else
    return $output;
    }

    This works. Changes in core files is not ideal of course, so I’ll probably have to copy a lot of code and turn it into a custom version in functions.php.

    Or is there a cleaner way? Is it possible to put something in functions.php that “catches” any calls for function previous_posts() and redirects them to a function custom_previous_posts() in functions.php?

Viewing 12 replies - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.