Neels
Neels

Reputation: 2543

How to redirect BuddyPress Menu Item to a different page on the Wordpress site?

So I am working on a website, and I am adding a new Menu item on my BuddyPress profile page. The Menu has been added correctly through a bp-custom.php page. But when I click on the Menu, I am not able to redirect it to the page I want to. The code is something like:

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/>';
}

How can I redirect it to a new page on the website irrespective of the root user domain?

Upvotes: 0

Views: 657

Answers (3)

Neels
Neels

Reputation: 2543

I had to remove the template functions, to redirect it properly. And as suggested by shanebp, the bp_core_redirect function came handy. The code now looks like this:

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/' );

}

Upvotes: 0

shanebp
shanebp

Reputation: 1956

Change this:

//  'parent_url'            => get_option('siteurl').'/shop',
//  'parent_slug'           => $bp->profile->slug,

To this:

'parent_url'            => $bp->displayed_user->domain,
'parent_slug'           => $bp->profile->slug,

Then try this:

function gift_card_screen_content() { 
     bp_core_redirect( site_url( '/shop/' ) );
}

Upvotes: 1

buley
buley

Reputation: 29238

What you need to do is issue a HTTP header with a Location key and value. WordPress has a function that will do this for you.

The function you're looking for is wp_redirect(). The canonical example:

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

It's not black magic, and basically does this:

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

But it's best to use WordPress related functions in a WordPress environment because developers like to respond to these kinds of actions and filter various related data.

Upvotes: 0

Related Questions