Thursday, March 14, 2024
HomeMobile MarketingWordPress: Dynamically Redirect An AMP Web page To Non-AMP When Unsupported

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


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 search engines like google. My theme helps AMP in posts (or {custom} put up sorts which might be a put up sort) however doesn’t assist AMP on a web page template.

Since Google doesn’t understand 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 revealed a web page with my hottest month-to-month posts. Google tried to index an AMP model at https://martech.zone/fashionable/?amp=1, leading to a script error and an error.

Quite than ignoring the error, I added code to my youngster theme features.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 mother or father web page
add_action('template_redirect', 'redirect_amp_to_non_amp');
perform redirect_amp_to_non_amp() {
    // Examine 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
        international $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 acceptable 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 needs 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 put up or different put up 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 out there, and the present request is for an AMP model of a web page.
  4. international $wp;
    • This line makes the worldwide variable $wp accessible inside 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 positioning.
    • add_query_arg(array(), $wp->request): Provides a question argument to the URL. On this case, an empty array is handed, that means no extra question arguments are added, but it surely 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 person 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 helpful for sustaining website positioning worth by making certain search engines like google 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 intrude with the redirect.

Redirect Customized Web page Template to Non-AMP

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

For a Particular Web page Template:

If you would like the redirection to use solely to a selected 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 appear to be this:

perform redirect_amp_to_non_amp() {
    // Examine 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()) {
        international $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 would like the redirection to use solely to a web page with a selected ID, you’ll 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 appear to be this:

perform redirect_amp_to_non_amp() {
    // Examine 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()) {
        international $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 inside the perform, you possibly can management exactly which pages or web page templates ought to set off the AMP to non-AMP redirection.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments