所以我正在开发一个网站,并且正在我的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/>';
}
如何将其重定向到网站上的新页面,而不考虑根用户域?
更改以下内容:
// '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/' );
}