Skip to main content

Access Denied Redirect

Vasyl MartyniukAbout 1 min

Definition

class AAM_Framework_Service_AccessDeniedRedirect {

    public set_redirect(string $area, array $redirect) : bool
    public get_redirect(string $area = null) : array
    public reset(string $area = null) : bool

}

set_redirect

Define how to handle user workflow when access is denied to restricted resource (e.g. post, URL or admin page) for a given website area. AAM recognizes 3 distinct website areas - frontend, backend and api; and you can define access denied redirect for each area separately.

The $redirect argument is an array with set of properties as following:

array (
    'type'               => string,
    'message'            => string,
    'redirect_page_id'   => int,
    'redirect_page_slug' => string,
    'redirect_url'       => string,
    'callback'           => string
)
PropertyDescription
typeRequired. Specifies the type of redirect. Accepted types are listed below.
messageRequired if type is "custom_message". Can contain plain text or HTML to display a message.
redirect_page_idRequired if type is "page_redirect". Must reference a valid, existing page ID.
redirect_page_slugRequired if type is "page_redirect". Must reference a valid, existing page slug.
redirect_urlRequired if type is "url_redirect". Must provide a valid absolute URL (e.g., https://example.com/path) or a relative path (e.g., /path).
callbackRequired if type is "trigger_callback". Must be a valid PHP callback function verified using is_callableopen in new window.

The accepted redirect types are:

  • default: show WordPress code wp_die message with "Access is denied" text
  • custom_message: show WordPress code wp_die message with custom text or HTML
  • login_redirect: redirect unauthenticated user to the login page
  • page_redirect: redirect to a page
  • url_redirect: safely redirect to URL
  • trigger_callback: trigger valid callback function that either handles redirect or returns safe URL for redirect.

Note!

Due to the nature of RESTful API workflow, the accepted redirect types are only default, custom_message and trigger_callback.

The following code defines a couple of different access denied redirects.

// Settings access denied redirect for the frontend area. Visitor will be redirected
// to the login page first and upon successful authentication - redirected back to
// the initial page.
AAM::api()->visitor()->access_denied_redirect()->set_redirect('frontend', [
    'type' => 'login_redirect'
]);

// Define access denied redirect for users with Subscriber role and redirect them to
// a specific landing page
AAM::api()->access_denied_redirect('role:subscriber')->set_redirect('frontend', [
    'type'         => 'url_redirect',
    'redirect_url' => '/upgrade-your-plan'
]);

get_redirect

Get currently defined access denied redirect for given area. If no redirect is defined, the method will return an array with type equals default.

// Get current redirect configuration for the backend area for user with email
// george@example.xyz
AAM::api()->access_denied_redirect('user:george@example.xyz')->get_redirect('backend');
Virtual Assistant