WordPress: Dynamically Redirect An AMP Web page To Non-AMP When Unsupported

0
2


I’ve AMP loaded on my website and have seen a pleasant stream of AMP visits from Google. Whereas I’m not an enormous fan of AMP, it does appear to garner fairly a little bit of consideration from serps. My theme helps AMP in posts (or {custom} submit varieties which can be a submit sort) however doesn’t assist AMP on a web page template.

Since Google doesn’t notice this, they report errors with the AMP path on these pages in Google Search Console. And… rightly so, the pages produce a 500 error. I found this after I printed a web page with my hottest month-to-month posts. Google tried to index an AMP model at https://martech.zone/common/?amp=1, leading to a script error and an error.

Somewhat than ignoring the error, I added code to my little one theme capabilities.php web page that redirects the customer in the event that they’re requesting an AMP web page, and the template is a web page template:

// If there is a web page URL with amp, redirect it to the father or mother web page
add_action('template_redirect', 'redirect_amp_to_non_amp');
perform redirect_amp_to_non_amp() {
    // Test if this can be a web page and if it is an AMP request
    if (is_page() && function_exists('amp_is_request') && amp_is_request()) {
        // Redirect to the non-AMP model of the web page
        world $wp;
        $current_url = home_url(add_query_arg(array(), $wp->request));
        wp_redirect($current_url, 301);
        exit;
    }
}

Code Breakdown:

  1. add_action(‘template_redirect’, ‘redirect_amp_to_non_amp’);
    • This line hooks a {custom} perform redirect_amp_to_non_amp into WordPress’s template_redirect motion. The template_redirect hook is executed simply earlier than WordPress determines which template or file ought to deal with the request. That is an applicable stage to carry out redirects.
  2. perform redirect_amp_to_non_amp() {…}
    • Right here, a perform named redirect_amp_to_non_amp is outlined. This perform accommodates the logic for checking whether or not the present request is for an AMP web page and whether or not it ought to be redirected.
  3. if (is_page() && function_exists(‘amp_is_request’) && amp_is_request()) {…}
    • Inside the perform, this conditional assertion checks three issues:
      • is_page(): Determines if the present request is for a WordPress web page (versus a submit or different submit sort).
      • function_exists('amp_is_request'): Checks whether or not the perform amp_is_request exists. This perform is a part of the AMP plugin and checks whether or not the present request is for an AMP web page.
      • amp_is_request(): If the perform exists, it’s then referred to as to find out if the present request is definitely for an AMP web page.
    • The whole situation will likely be true if the request is for a web page, the amp_is_request perform is on the market, and the present request is for an AMP model of a web page.
  4. world $wp;
    • This line makes the worldwide variable $wp obtainable throughout the perform. The $wp variable is an occasion of the WP class and accommodates properties associated to the present request, together with the request string.
  5. $current_url = home_url(add_query_arg(array(), $wp->request));
    • home_url(): This WordPress perform retrieves the house URL of the location.
    • add_query_arg(array(), $wp->request): Provides a question argument to the URL. On this case, an empty array is handed, that means no further question arguments are added, however it successfully rebuilds the present request URI with none question parameters (like ?amp=1).
    • The result’s the present URL with none AMP-related question parameters.
  6. wp_redirect($current_url, 301);
    • This perform redirects the consumer to the non-AMP model of the web page (contained in $current_url) with a 301 HTTP standing code, indicating a everlasting redirect. That is useful for sustaining search engine marketing worth by guaranteeing serps replace their index to the non-AMP URL.
  7. exit;
    • This command is used to terminate the execution of the script instantly after the redirect is initiated. It prevents WordPress from persevering with to load the unique AMP web page or executing additional code that would intervene with the redirect.

Redirect Customized Web page Template to Non-AMP

You may modify the code to use it to a particular web page template or web page ID as nicely.

For a Particular Web page Template:

If you need the redirection to use solely to a particular web page template, you’ll use the is_page_template() perform in your conditional test. For instance, in case your template is known as custom-template.php, the code would seem like this:

perform redirect_amp_to_non_amp() {
    // Test if this can be a particular template, the perform exists, and it is an AMP request
    if (is_page_template('custom-template.php') && function_exists('amp_is_request') && amp_is_request()) {
        world $wp;
        $current_url = home_url(add_query_arg(array(), $wp->request));
        wp_redirect($current_url, 301);
        exit;
    }
}
add_action('template_redirect', 'redirect_amp_to_non_amp');

On this code, is_page_template('custom-template.php') checks whether or not the present web page makes use of the custom-template.php template.

For a Particular Web page ID:

If you need the redirection to use solely to a web page with a particular ID, you’d use the is_page() perform with the precise ID as its parameter. For instance, if you wish to apply this to a web page with the ID of 42, the code would seem like this:

perform redirect_amp_to_non_amp() {
    // Test if this can be a particular web page ID, the perform exists, and it is an AMP request
    if (is_page(42) && function_exists('amp_is_request') && amp_is_request()) {
        world $wp;
        $current_url = home_url(add_query_arg(array(), $wp->request));
        wp_redirect($current_url, 301);
        exit;
    }
}
add_action('template_redirect', 'redirect_amp_to_non_amp');

By tailoring the conditional assertion throughout the perform, you possibly can management exactly which pages or web page templates ought to set off the AMP to non-AMP redirection.

LEAVE A REPLY

Please enter your comment!
Please enter your name here