bp_core_no_access( array|string $args = '' )

Redirect a user to log in for BP pages that require access control.

Description

Add an error message (if one is provided).

If authenticated, redirects user back to requested content by default.

Parameters

$args

(Optional)

  • 'mode'
    (int) Specifies the destination of the redirect. 1 will direct to the root domain (home page), which assumes you have a log-in form there; 2 directs to wp-login.php. Default: 2.
  • 'redirect'
    (string) The URL the user will be redirected to after successfully logging in. Default: the URL originally requested.
  • 'root'
    (string) The root URL of the site, used in case of error or mode 1 redirects. Default: the value of bp_get_root_domain().
  • 'message'
    (string) An error message to display to the user on the log-in page. Default: "You must log in to access the page you requested."

Default value: ''

Source

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

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
function bp_core_no_access( $args = '' ) {
 
    // Build the redirect URL.
    $redirect_url  = is_ssl() ? 'https://' : 'http://';
    $redirect_url .= $_SERVER['HTTP_HOST'];
    $redirect_url .= $_SERVER['REQUEST_URI'];
 
    $defaults = array(
        'mode'     => 2,                    // 1 = $root, 2 = wp-login.php.
        'redirect' => $redirect_url,        // the URL you get redirected to when a user successfully logs in.
        'root'     => bp_get_root_domain(), // the landing page you get redirected to when a user doesn't have access.
        'message'  => __( 'Please login to access this website.', 'buddyboss' )
    );
 
    $r = wp_parse_args( $args, $defaults );
 
    /**
     * Filters the arguments used for user redirecting when visiting access controlled areas.
     *
     * @since BuddyPress 1.6.0
     *
     * @param array $r Array of parsed arguments for redirect determination.
     */
    $r = apply_filters( 'bp_core_no_access', $r );
    extract( $r, EXTR_SKIP );
 
    /*
     * @ignore Ignore these filters and use 'bp_core_no_access' above.
     */
    $mode     = apply_filters( 'bp_no_access_mode',     $mode,     $root,     $redirect, $message );
    $redirect = apply_filters( 'bp_no_access_redirect', $redirect, $root,     $message$mode    );
    $root     = apply_filters( 'bp_no_access_root',     $root,     $redirect, $message$mode    );
    $message  = apply_filters( 'bp_no_access_message'$message$root,     $redirect, $mode    );
    $root     = trailingslashit( $root );
 
    switch ( $mode ) {
 
        // Option to redirect to wp-login.php.
        // Error message is displayed with bp_core_no_access_wp_login_error().
        case 2 :
            if ( !empty( $redirect ) ) {
                bp_core_redirect( add_query_arg( array(
                    'bp-auth' => 1,
                    'action'  => 'bpnoaccess'
                ), wp_login_url( $redirect ) ) );
            } else {
                bp_core_redirect( $root );
            }
 
            break;
 
        // Redirect to root with "redirect_to" parameter.
        // Error message is displayed with bp_core_add_message().
        case 1 :
        default :
 
            $url = $root;
            if ( !empty( $redirect ) ) {
                $url = add_query_arg( 'redirect_to', urlencode( $redirect ), $root );
            }
 
            if ( !empty( $message ) ) {
                bp_core_add_message( $message, 'error' );
            }
 
            bp_core_redirect( $url );
 
            break;
    }
}

Changelog

Changelog
Version Description
BuddyPress 1.5.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.