简体   繁体   English

如何在BuddyPress和WordPress中自定义用户URL?

[英]How to custom user url in BuddyPress and WordPress?

Thanks for support! 感谢你的支持! I need to custom user url in my page using WordPress and BuddyPress. 我需要使用WordPress和BuddyPress在我的页面中自定义用户URL。 This is example: 这是一个例子:

From: (current) 来自:(当前)

http://example.com/user/pum_su411

To

http://example.com/user/548234

With 548234 is ID of the user. 548234是用户的ID。

I want after completed the custom, all users will have url like above automatically. 我希望在完成自定义后,所有用户都会自动拥有上面的网址。

Thanks for all solutions! 感谢所有解决方案!

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. 花了一些时间浏览BuddyPress的文档,codex和文件,我可以找到更改url的/user/部分的方法,但遗憾的是它不是/username的一部分。

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. 通过阅读,它被控制在BuddyPress的核心内,并且对核心的任何更改都可能导致崩溃,并且更有可能导致问题或进一步覆盖。

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. 这并不是说它不可能,但它肯定是可能的,但它需要对许多不同的文件进行大量编辑,对许多BuddyPress功能进行编辑,并且无法保证它直接工作当文件得到更新时,甚至在线下工作。

I would recommend going onto the BuddyPress Trac and putting in a ticket to have the feature added to change user url structure. 我建议你去BuddyPress Trac并加入一张票,以便添加该功能以更改用户URL结构。 It would be a cool feature to be able to swap between a username, full name, ID or any other unique identifiable string. 能够在用户名,全名,ID或任何其他唯一可识别字符串之间进行交换将是一个很酷的功能。

You can access it here: https://buddypress.trac.wordpress.org/ 你可以在这里访问它: 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. 或者,您可以尝试aSeptik上面所做的事情,但确保在BuddyPress更新时更新该文件的任何更改。

  • add this code to your theme functions.php file. 将此代码添加到主题functions.php文件中。
    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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM