![](/img/trans.png)
[英]How to redirect author URL query to buddypress user profile page in Wordpress
[英]How to custom user url in BuddyPress and WordPress?
感謝你的支持! 我需要使用WordPress和BuddyPress在我的頁面中自定義用戶URL。 這是一個例子:
來自:(當前)
http://example.com/user/pum_su411
至
http://example.com/user/548234
548234
是用戶的ID。
我希望在完成自定義后,所有用戶都會自動擁有上面的網址。
感謝所有解決方案!
花了一些時間瀏覽BuddyPress的文檔,codex和文件,我可以找到更改url的/user/
部分的方法,但遺憾的是它不是/username
的一部分。
通過閱讀,它被控制在BuddyPress的核心內,並且對核心的任何更改都可能導致崩潰,並且更有可能導致問題或進一步覆蓋。
這並不是說它不可能,但它肯定是可能的,但它需要對許多不同的文件進行大量編輯,對許多BuddyPress功能進行編輯,並且無法保證它直接工作當文件得到更新時,甚至在線下工作。
我建議你去BuddyPress Trac並加入一張票,以便添加該功能以更改用戶URL結構。 能夠在用戶名,全名,ID或任何其他唯一可識別字符串之間進行交換將是一個很酷的功能。
你可以在這里訪問它: https : //buddypress.trac.wordpress.org/
或者,您可以嘗試aSeptik上面所做的事情,但確保在BuddyPress更新時更新該文件的任何更改。
- 將此代碼添加到主題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.