Not Found (404) Redirect
Less than 1 minute
Definition
class AAM_Framework_Service_NotFoundRedirect {
public set_redirect(array $redirect) : bool
public get_redirect() : array
public reset() : bool
}
set_redirect
Define how to handle user workflow when 404 is detected.
The $redirect argument is an array with set of properties as following:
array (
'type' => 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. |
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" textlogin_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.
The following code redirects visitors to custom landing page:
AAM::api()->not_found_redirect('visitor')->set_redirect([
'type' => 'page_redirect',
'redirect_page_slug' => 'knowledge-base'
]);
get_redirect
Get currently defined 404 redirect for visitors. If no redirect is defined, the method will return an array with type equals default.
AAM::api()->not_found_redirect('visitor')->get_redirect();