[英]How to redirect after login to BuddyPress profile in WordPress
[英]How to redirect BuddyPress Menu Item to a different page on the Wordpress site?
因此,我正在一個網站上工作,並在BuddyPress個人資料頁面上添加了一個新的菜單項。 該菜單已通過bp-custom.php
頁面正確添加。 但是,當我單擊菜單時,無法將其重定向到想要的頁面。 代碼類似於:
function add_gift_card() {
global $bp;
bp_core_new_nav_item( array(
'name' => 'Gift Cards',
'slug' => 'shop',
// 'parent_url' => get_option('siteurl').'/shop',
// 'parent_slug' => $bp->profile->slug,
'screen_function' => 'gift_card_screen',
'position' => 90,
'default_subnav_slug' => 'shop'
) );
}
add_action( 'bp_setup_nav', 'add_gift_card', 100 );
function gift_card_screen() {
add_action( 'bp_template_content', 'gift_card_screen_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function gift_card_screen_content() {
echo 'Gift Cards<br/>';
}
不論root用戶域如何,如何將其重定向到網站上的新頁面?
更改此:
// 'parent_url' => get_option('siteurl').'/shop',
// 'parent_slug' => $bp->profile->slug,
對此:
'parent_url' => $bp->displayed_user->domain,
'parent_slug' => $bp->profile->slug,
然后試試這個:
function gift_card_screen_content() {
bp_core_redirect( site_url( '/shop/' ) );
}
您需要執行的操作是發出帶有Location
鍵和值的HTTP標頭。 WordPress具有可以為您完成此功能的功能。
您要查找的函數是wp_redirect()
。 典型的例子:
<?php
wp_redirect( $location, $status );
exit;
?>
這不是黑魔法,基本上是這樣做的:
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.anyhost.com");
但是最好在WordPress環境中使用與WordPress相關的功能,因為開發人員喜歡響應此類操作並過濾各種相關數據。
我必須刪除模板功能,才能對其進行正確的重定向。 正如shanebp所建議的那樣,bp_core_redirect函數非常方便。 現在的代碼如下所示:
function add_gift_card() {
global $bp;
bp_core_new_nav_item( array(
'name' => 'Gift Cards',
'slug' => 'shop',
'screen_function' => 'gift_card_screen',
'position' => 90,
'default_subnav_slug' => 'shop'
) );
}
add_action( 'bp_setup_nav', 'add_gift_card', 100 );
function gift_card_screen() {
bp_core_redirect( get_option('siteurl').'/shop/' );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.