Skip to main content

Not Found (404) Redirect

Vasyl MartyniukLess 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
)
PropertyDescription
typeRequired. Specifies the type of redirect. Accepted types are listed below.
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
  • 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.

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();
Virtual Assistant