代码之家  ›  专栏  ›  技术社区  ›  Neels Humayun

如何将BuddyPress菜单项重定向到Wordpress网站上的其他页面?

  •  0
  • Neels Humayun  · 技术社区  · 9 年前

    所以我正在一个网站上工作,我正在我的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/>';
    }
    

    无论根用户域如何,我如何将其重定向到网站上的新页面?

    3 回复  |  直到 9 年前
        1
  •  1
  •   shanebp    9 年前

    更改此设置:

    //  '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/' ) );
    }
    
        2
  •  0
  •   buley    9 年前

    您需要做的是使用 Location 键和值。WordPress有一个功能可以帮你完成这项工作。

    您正在寻找的功能是 wp_redirect() 。典型示例:

    <?php
        wp_redirect( $location, $status );
        exit;
    ?>
    

    这不是黑魔法,基本上是这样的:

    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: http://www.anyhost.com"); 
    

    但最好在WordPress环境中使用与WordPres相关的功能,因为开发人员喜欢响应这些操作并过滤各种相关数据。

        3
  •  0
  •   Neels Humayun    9 年前

    我必须删除模板函数,才能正确地重定向它。正如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/' );
    
    }