Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

WordPress: Dynamically Redirect an AMP Page To Non-AMP When Unsupported

I have AMP loaded on my site and have seen a nice stream of AMP visits from Google. While I’m not a huge fan of AMP, it does seem to garner quite a bit of attention from search engines. My theme supports AMP in posts (or custom post types that are a post type) but doesn’t support AMP on a page template.

Since Google doesn’t realize 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 discovered this when I published a page with my most popular monthly posts. Google tried to index an AMP version at https://martech.zone/popular/?amp=1, resulting in a script error and an error.

Rather than ignoring the error, I added code to my child theme functions.php page that redirects the visitor if they’re requesting an AMP page, and the template is a page template:

// If there's a page URL with amp, redirect it to the parent page
add_action('template_redirect', 'redirect_amp_to_non_amp');
function redirect_amp_to_non_amp() {
    // Check if this is a page and if it's an AMP request
    if (is_page() && function_exists('amp_is_request') && amp_is_request()) {
        // Redirect to the non-AMP version of the page
        global $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 function redirect_amp_to_non_amp into WordPress’s template_redirect action. The template_redirect hook is executed just before WordPress determines which template or file should handle the request. This is an appropriate stage to perform redirects.
  2. function redirect_amp_to_non_amp() {…}
    • Here, a function named redirect_amp_to_non_amp is defined. This function contains the logic for checking whether the current request is for an AMP page and whether it should be redirected.
  3. if (is_page() && function_exists(‘amp_is_request’) && amp_is_request()) {…}
    • Within the function, this conditional statement checks three things:
      • is_page(): Determines if the current request is for a WordPress page (as opposed to a post or other post type).
      • function_exists('amp_is_request'): Checks whether the function amp_is_request exists. This function is part of the AMP plugin and checks whether the current request is for an AMP page.
      • amp_is_request(): If the function exists, it’s then called to determine if the current request is actually for an AMP page.
    • The entire condition will be true if the request is for a page, the amp_is_request function is available, and the current request is for an AMP version of a page.
  4. global $wp;
    • This line makes the global variable $wp available within the function. The $wp variable is an instance of the WP class and contains properties related to the current request, including the request string.
  5. $current_url = home_url(add_query_arg(array(), $wp->request));
    • home_url(): This WordPress function retrieves the home URL of the site.
    • add_query_arg(array(), $wp->request): Adds a query argument to the URL. In this case, an empty array is passed, meaning no additional query arguments are added, but it effectively rebuilds the current request URI without any query parameters (like ?amp=1).
    • The result is the current URL without any AMP-related query parameters.
  6. wp_redirect($current_url, 301);
    • This function redirects the user to the non-AMP version of the page (contained in $current_url) with a 301 HTTP status code, indicating a permanent redirect. This is beneficial for maintaining SEO value by ensuring search engines update their index to the non-AMP URL.
  7. exit;
    • This command is used to terminate the execution of the script immediately after the redirect is initiated. It prevents WordPress from continuing to load the original AMP page or executing further code that could interfere with the redirect.

Redirect Custom Page Template to Non-AMP

You can modify the code to apply it to a specific page template or page ID as well.

For a Specific Page Template:

If you want the redirection to apply only to a specific page template, you will use the is_page_template() function in your conditional check. For example, if your template is named custom-template.php, the code would look like this:

function redirect_amp_to_non_amp() {
    // Check if this is a specific template, the function exists, and it's an AMP request
    if (is_page_template('custom-template.php') && function_exists('amp_is_request') && amp_is_request()) {
        global $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');

In this code, is_page_template('custom-template.php') checks whether the current page uses the custom-template.php template.

For a Specific Page ID:

If you want the redirection to apply only to a page with a specific ID, you would use the is_page() function with the specific ID as its parameter. For example, if you want to apply this to a page with the ID of 42, the code would look like this:

function redirect_amp_to_non_amp() {
    // Check if this is a specific page ID, the function exists, and it's an AMP request
    if (is_page(42) && function_exists('amp_is_request') && amp_is_request()) {
        global $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 statement within the function, you can control precisely which pages or page templates should trigger the AMP to non-AMP redirection.

©2024 DK New Media, LLC, All rights reserved.

Originally Published on Martech Zone: WordPress: Dynamically Redirect an AMP Page To Non-AMP When Unsupported



This post first appeared on How To Optimize Prestashop For Increased SEO And Conversions, please read the originial post: here

Share the post

WordPress: Dynamically Redirect an AMP Page To Non-AMP When Unsupported

×

Subscribe to How To Optimize Prestashop For Increased Seo And Conversions

Get updates delivered right to your inbox!

Thank you for your subscription

×