bp_core_maybe_hook_new_subnav_screen_function( array $subnav_item, string $component = 'members' )

For a given subnav item, either hook the screen function or generate redirect arguments, as necessary.

Description

Parameters

$subnav_item

(Required) The subnav array added to the secondary navigation of the component in bp_core_new_subnav_item().

$component

(Optional) The component the navigation is attached to. Defaults to 'members'.

Default value: 'members'

Return

(array)

Source

File: bp-core/bp-core-buddybar.php

655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
function bp_core_maybe_hook_new_subnav_screen_function( $subnav_item, $component = 'members' ) {
    $retval = array(
        'status' => '',
    );
 
    // Is this accessible by site admins only?
    $site_admin_restricted = false;
    if ( ! empty( $subnav_item['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) {
        $site_admin_restricted = true;
    }
 
    // User has access, so let's try to hook the display callback.
    if ( ! empty( $subnav_item['user_has_access'] ) && ! $site_admin_restricted ) {
 
        // Screen function is invalid.
        if ( ! is_callable( $subnav_item['screen_function'] ) ) {
            $retval['status'] = 'failure';
 
        // Success - hook to bp_screens.
        } else {
            add_action( 'bp_screens', $subnav_item['screen_function'], 3 );
            $retval['status'] = 'success';
        }
 
    // User doesn't have access. Determine redirect arguments based on
    // user status.
    } else {
        $retval['status'] = 'failure';
 
        if ( is_user_logged_in() ) {
 
            $bp = buddypress();
 
            // If a redirect URL has been passed to the subnav item, respect it.
            if ( ! empty( $subnav_item['no_access_url'] ) ) {
                $message     = __( 'You do not have access to this page.', 'buddyboss' );
                $redirect_to = trailingslashit( $subnav_item['no_access_url'] );
 
            // In the case of a user page, we try to assume a
            // redirect URL.
            } elseif ( bp_is_user() ) {
 
                $parent_nav_default = $bp->{$component}->nav->get_primary( array( 'slug' => $bp->default_component ), false );
                if ( $parent_nav_default ) {
                    $parent_nav_default_item = reset( $parent_nav_default );
                }
 
                // Redirect to the displayed user's default
                // component, as long as that component is
                // publicly accessible.
                if ( bp_is_my_profile() || ( isset( $parent_nav_default_item ) && $parent_nav_default_item->show_for_displayed_user ) ) {
                    $message     = __( 'You do not have access to this page.', 'buddyboss' );
                    $redirect_to = bp_displayed_user_domain();
 
                // In some cases, the default tab is not accessible to
                // the logged-in user. So we fall back on a tab that we
                // know will be accessible.
                } else {
                    // Try 'activity' first.
                    if ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) {
                        $redirect_to = trailingslashit( bp_displayed_user_domain() . bp_get_activity_slug() );
                    // Then try 'profile'.
                    } else {
                        $redirect_to = trailingslashit( bp_displayed_user_domain() . ( 'xprofile' == $bp->profile->id ? 'profile' : $bp->profile->id ) );
                    }
 
                    $message     = '';
                }
 
            // Fall back to the home page.
            } else {
                $message     = __( 'You do not have access to this page.', 'buddyboss' );
                $redirect_to = bp_get_root_domain();
            }
 
            $retval['redirect_args'] = array(
                'message'  => $message,
                'root'     => $redirect_to,
                'redirect' => false,
                'mode'     => 1
            );
 
        } else {
            // When the user is logged out, pass an empty array
            // This indicates that the default arguments should be
            // used in bp_core_no_access().
            $retval['redirect_args'] = array();
        }
    }
 
    return $retval;
}

Changelog

Changelog
Version Description
BuddyPress 2.1.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.