bp_core_no_access

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

Description

bp_core_no_access( (string) $args = '' ); 

Add an error message (if one is provided).

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

Parameters (1)

0. $args — Optional. (string) => ''
The args.

Options

  • mode (int) => 0

    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.

array(

    /**
     * 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.
     *
     * @type int
     */
    'mode' => 0,

    /**
     * The URL the user will be redirected to after successfully logging in. Default: the URL
     * originally requested.
     *
     * @type string
     * @default ''
     */
    'redirect' => ''
);        


Usage

  1. if ( !function_exists( 'bp_core_no_access' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-catchuri.php'; 
  3.  
  4. // The args. 
  5. $args = array( 
  6. 'mode' => 0, 
  7. 'redirect' => '' 
  8. ); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_core_no_access($args); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-catchuri.php  
  1. function bp_core_no_access( $args = '' ) { 
  2.  
  3. // Build the redirect URL. 
  4. $redirect_url = is_ssl() ? 'https://' : 'http://'; 
  5. $redirect_url .= $_SERVER['HTTP_HOST']; 
  6. $redirect_url .= $_SERVER['REQUEST_URI']; 
  7.  
  8. $defaults = array( 
  9. 'mode' => 2, // 1 = $root, 2 = wp-login.php. 
  10. 'redirect' => $redirect_url, // the URL you get redirected to when a user successfully logs in. 
  11. 'root' => bp_get_root_domain(), // the landing page you get redirected to when a user doesn't have access. 
  12. 'message' => __( 'You must log in to access the page you requested.', buddypress ) 
  13. ); 
  14.  
  15. $r = wp_parse_args( $args, $defaults ); 
  16.  
  17. /** 
  18. * Filters the arguments used for user redirecting when visiting access controlled areas. 
  19. * @since 1.6.0 
  20. * @param array $r Array of parsed arguments for redirect determination. 
  21. */ 
  22. extract( $r, EXTR_SKIP ); 
  23.  
  24. /** 
  25. * @ignore Ignore these filters and use 'bp_core_no_access' above. 
  26. */ 
  27. $mode = apply_filters( 'bp_no_access_mode', $mode, $root, $redirect, $message ); 
  28. $redirect = apply_filters( 'bp_no_access_redirect', $redirect, $root, $message, $mode ); 
  29. $root = apply_filters( 'bp_no_access_root', $root, $redirect, $message, $mode ); 
  30. $message = apply_filters( 'bp_no_access_message', $message, $root, $redirect, $mode ); 
  31. $root = trailingslashit( $root ); 
  32.  
  33. switch ( $mode ) { 
  34.  
  35. // Option to redirect to wp-login.php. 
  36. // Error message is displayed with bp_core_no_access_wp_login_error(). 
  37. case 2 : 
  38. if ( !empty( $redirect ) ) { 
  39. bp_core_redirect( add_query_arg( array( 'action' => 'bpnoaccess' ), wp_login_url( $redirect ) ) ); 
  40. } else { 
  41. bp_core_redirect( $root ); 
  42.  
  43. break; 
  44.  
  45. // Redirect to root with "redirect_to" parameter. 
  46. // Error message is displayed with bp_core_add_message(). 
  47. case 1 : 
  48. default : 
  49.  
  50. $url = $root; 
  51. if ( !empty( $redirect ) ) { 
  52. $url = add_query_arg( 'redirect_to', urlencode( $redirect ), $root ); 
  53.  
  54. if ( !empty( $message ) ) { 
  55. bp_core_add_message( $message, error ); 
  56.  
  57. bp_core_redirect( $url ); 
  58.  
  59. break;