bp_core_no_access( array|string $args = '' )

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


Description Description

Add an error message (if one is provided).

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


Top ↑

Parameters Parameters

$args

(array|string) (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 Source

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

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'  => __( 'You must log in to access the page you requested.', 'buddypress' )
	);

	$r = wp_parse_args( $args, $defaults );

	/**
	 * Filters the arguments used for user redirecting when visiting access controlled areas.
	 *
	 * @since 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;
	}
}


Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.