Access Denied Redirect
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
)
| Property | Description |
|---|---|
type | Required. Specifies the type of redirect. Accepted types are listed below. |
message | Required if type is "custom_message". Can contain plain text or HTML to display a message. |
redirect_page_id | Required if type is "page_redirect". Must reference a valid, existing page ID. |
redirect_page_slug | Required if type is "page_redirect". Must reference a valid, existing page slug. |
redirect_url | Required if type is "url_redirect". Must provide a valid absolute URL (e.g., https://example.com/path) or a relative path (e.g., /path). |
callback | Required if type is "trigger_callback". Must be a valid PHP callback function verified using is_callable. |
The accepted redirect types are:
default: show WordPress codewp_diemessage with "Access is denied" textcustom_message: show WordPress codewp_diemessage with custom text or HTMLlogin_redirect: redirect unauthenticated user to the login pagepage_redirect: redirect to a pageurl_redirect: safely redirect to URLtrigger_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');