0
votes

Thanks for support! I need to custom user url in my page using WordPress and BuddyPress. This is example:

From: (current)

http://example.com/user/pum_su411

To

http://example.com/user/548234

With 548234 is ID of the user.

I want after completed the custom, all users will have url like above automatically.

Thanks for all solutions!

2
Voted up because buddypress questions on wordpress.stackexchange.com are marked as off-topicshanebp

2 Answers

1
votes
  • add this code to your theme functions.php file.
    function _bp_core_get_user_domain($domain, $user_id, $user_nicename = false, $user_login = false) {
        if ( empty( $user_id ) ){
            return;
        }
        if( isset($user_nicename) ){
            $user_nicename = bp_core_get_username($user_id);
        }
        $after_domain =  bp_get_members_root_slug() . '/' . $user_id;

        $domain = trailingslashit( bp_get_root_domain() . '/' . $after_domain );
        $domain = apply_filters( 'bp_core_get_user_domain_pre_cache', $domain, $user_id, $user_nicename, $user_login );
        if ( !empty( $domain ) ) {
            wp_cache_set( 'bp_user_domain_' . $user_id, $domain, 'bp' );
        }
        return $domain;
    }

    add_filter('bp_core_get_user_domain', '_bp_core_get_user_domain', 10, 4);

    function _bp_core_get_userid($userid, $username){
        if(is_numeric($username)){
            $aux = get_userdata( $username );
            if( get_userdata( $username ) )
                $userid = $username;
        }
        return $userid;
    }

    add_filter('bp_core_get_userid', '_bp_core_get_userid', 10, 2);

    function _bp_get_activity_parent_content($content){
        global $bp;
        $user = get_user_by('slug', $bp->displayed_user->fullname); // 'slug' - user_nicename
        return preg_replace('/href=\"(.*?)\"/is', 'href="'.bp_core_get_user_domain($user->ID, $bp->displayed_user->fullname).'"', $content);
    }

    add_filter( 'bp_get_activity_parent_content','_bp_get_activity_parent_content', 10, 1 );

    function _bp_get_activity_action_pre_meta($content){
        global $bp;
        $fullname = $bp->displayed_user->fullname; // 'slug' - user_nicename
        $user = get_user_by('slug', $fullname);
        if(!is_numeric($user->ID) || empty($fullname)){
            $args = explode(' ', trim(strip_tags($content)));
            $fullname = trim($args[0]);
            $user = get_user_by('slug', $fullname);
        }
        return preg_replace('/href=\"(.*?)\"/is', 'href="'.bp_core_get_user_domain($user->ID, $fullname).'"', $content);
    }

    add_action('bp_get_activity_action_pre_meta', '_bp_get_activity_action_pre_meta');

    add_filter('bp_core_get_userid_from_nicename', '_bp_core_get_userid', 10, 2);
1
votes

Just spent a bit of time going over the documentation, codex and files of BuddyPress and i can find ways of changing the /user/ part of the url but sadly not the /username side of it.

Reading through, it is controlled within the core of BuddyPress and any changes to the core can cause crashes and more than likely to cause problems or overwrites further down the line.

This isn't to say it's not possible though, it is most certainly possible, but it will require a great deal of editing to many many different files, an edit to a number of BuddyPress functions and there are no guarantee's on it working straight out or even working further down the line when files get update.

I would recommend going onto the BuddyPress Trac and putting in a ticket to have the feature added to change user url structure. It would be a cool feature to be able to swap between a username, full name, ID or any other unique identifiable string.

You can access it here: https://buddypress.trac.wordpress.org/

Alternatively, you can try what aSeptik has done above, but make sure to update that file with any changes when BuddyPress updates as well.