• Resolved Jéssica

    (@jbanosr)


    Hi,

    I have issue with hreflang. I think that the AMP plugin is not generating properly hreflang because Semrush shows me “No self-referencing hreflang” error from almost all pages are in AMP.

    Images:
    https://ibb.co/dgKcN5K
    https://ibb.co/mt99M9y

    Support UUID: ampwp-472d7e7c-7c13-55cd-bacc-73d39803d895

    Could you help, me please?

    Best regards,

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Milind More

    (@milindmore22)

    Hello @jbanosr

    Thank you for reaching out to us, Yes hreflang attribute was not being added to link tags added by AMP plugin, can you please try adding the following code snippet at end of your child themes function.php file

    /**
     * Add amphtml link.
     *
     * If there are known validation errors for the current URL then do not output anything.
     *
     * @since 1.0
     */
    function amp_add_amphtml_link_hreflang() {
    	if ( function_exists( 'amp_is_canonical' ) && ! amp_is_canonical() ) {
    		$amp_url = amp_add_paired_endpoint( amp_get_current_url() );
    		if ( $amp_url ) {
    			$amp_url          = remove_query_arg( 'noamp', $amp_url );
    			printf( '<link rel="amphtml" href="%s" hreflang="%s">', esc_url( $amp_url ), get_locale() );
    		}
    	}
    }
    
    add_action( 'wp_head', 'amp_add_amphtml_link_hreflang' );

    We hope this helps!

    Thread Starter Jéssica

    (@jbanosr)

    Hi Milind,

    I added the code and now the error is different. Could you help me, please? https://ibb.co/F8MJBKh

    Thank you so much for helping

    Plugin Support Milind More

    (@milindmore22)

    Hello @jbanosr

    I think it needs a language code in ISO-0639-1 format, since you are using polylang plugin let’s use the pll_current_language('slug');

    Please replace the previous code snippet with the updated one.

    /**
     * Add amphtml link.
     *
     * If there are known validation errors for the current URL then do not output anything.
     *
     * @since 1.0
     */
    function amp_add_amphtml_link_hreflang() {
    	if ( function_exists( 'amp_is_canonical' ) && ! amp_is_canonical() ) {
    		$amp_url = amp_add_paired_endpoint( amp_get_current_url() );
    		// Get the locale code.
    		$locale_code = get_locale();
    		// Covert to ISO 639-1 language code.
    		$locale_code = substr( $locale_code, 0, 2 );
    		
    		// ploylang support.
    		if( function_exists( 'pll_current_language' ) ) {
    			$locale_code = pll_current_language('slug');
    		}
    
    		if ( $amp_url ) {
    			$amp_url          = remove_query_arg( 'noamp', $amp_url );
    			printf( '<link rel="amphtml" href="%s" hreflang="%s">', esc_url( $amp_url ), $locale_code );
    		}
    	}
    }
    
    add_action( 'wp_head', 'amp_add_amphtml_link_hreflang' );
    Thread Starter Jéssica

    (@jbanosr)

    Hi Milind,

    I added the code and I have different issues:

    1- AMP pages are missing country issue. (es-ES, pt-PT, es-MX).
    2- Posts AMP version have “Canonical does not match hreflang href” and “Alternate does not link back issues.

    Image: https://ibb.co/ZxyXK24

    Thank you for helping

    Plugin Support Milind More

    (@milindmore22)

    Hello @jbanosr

    Those alternate links are being added by polylang plugin, it will be best to contact polylang support as they can guide you better regarding the code that has been added by thier plugin.

    Plugin Support Milind More

    (@milindmore22)

    Hello @jbanosr

    Sorry, I forgot to give you code that will remove the existing <link rel="amphtml"> tag in my previous response, please also add the following snippet

    add_action( 'template_redirect', static function () {
     	remove_action( 'wp_head', 'amp_add_amphtml_link' );
    } );
    Plugin Support Milind More

    (@milindmore22)

    @jbanosr As we didn’t receive a response I’ll mark this as resolved. Feel free to open a new support topic if you require any further assistance.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Problem with Hreflang’ is closed to new replies.