Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to shorten a profile link?

  • Is it possible to shorten a profile link such as:

    http://www.example.com/forums/profile/username

    to:

    http://www.example.com/profile/username

    or to:

    http://www.example.com/username

    or to:

    http://my.example.com/username

    or to:

    http://username.example.com

    Thanks in advance.

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

  • _ck_
    Participant

    @_ck_

    mod_rewrite will let you do that if you figure out the correct rule

    however you won’t be able to get it shorter than profile/username because just /username could be another page.

    I’ve tried mod_rewrite, but running into problems with the wp_redirect functions in bbPress redirecting the page after the URL has been rewritten, so that the URL changes back to the longer form.

    For example, this mod_rewrite rule:

    RewriteRule ^profile/username forums/profile/username [L]

    will rewrite:

    http://www.example.com/profile/username

    to:

    http://www.example.com/forums/profile/username

    Except, that after its been rewritten, bbPress forces a redirection, causing the URL in the browser window to change to the longer form.

    Okay, I think I got it working.

    I was able to fix the redirection issue by modifying the get_user_profile_link() function in the template-functions.php. I added the following line towards the end of the function, right before the last line of code in the function:

    $r = str_replace('forums/profile/' . $user->$column , "profile/" . $user->$column, $r);

    This is the full code:

    function get_user_profile_link( $id = 0, $page = 1 ) {
    $user = bb_get_user( bb_get_user_id( $id ) );
    $rewrite = bb_get_option( 'mod_rewrite' );
    if ( $rewrite ) {
    if ( $rewrite === 'slugs' ) {
    $column = 'user_nicename';
    } else {
    $column = 'ID';
    }
    $r = bb_get_option('uri') . "profile/" . $user->$column . ( 1 < $page ? "/page/$page" : '' );
    } else {
    $r = bb_get_option('uri') . "profile.php?id=$user->ID" . ( 1 < $page ? "&page=$page" : '' );
    }

    $r = str_replace('forums/profile/' . $user->$column , "profile/" . $user->$column, $r);

    return apply_filters( 'get_user_profile_link', $r, $user->ID );
    }

    I noticed that if you did not install bbPress with WordPress cookie integration, then this hack will cause bbPress to always think you are logged out. I think it has something with it not being able to retrieve the cookie because of the path change.

    However, if you have WordPress cookie integration enabled, then everything seems to work. Not exactly sure why. But I think it must be something to with the path that’s being set in the cookie.

    By the way, I made this hack cleaner by implementing it as a filter inside a plugin.

    function my_get_user_profile_link_filter( $link , $user_id = 0 ) {
    //check for rewrite
    $rewrite = bb_get_option( 'mod_rewrite' );
    if ( $rewrite ) {
    //what kind of rewrite there is? slug use "forum_slug" column, else the column is "forum_id"
    $column = ($rewrite === 'slugs')?('forum_slug'):('forum_id');
    $link = str_replace('forums/profile/', 'people/', $link);
    }
    return $link; // Very important line!
    }

    This was based on code suggested in another thread:

    https://bbpress.org/forums/topic/nicer-slug-url-rewrite-plugin-done

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.