• Resolved chris-slick

    (@chris-slick)


    Is there a way to force internal links to open the AMP versions of Posts? It is quite strange for a visitor to land on an AMP page then navigate using an internal link to find themselves on my websites template.

    An option to force AMP on mobile would also be quite handy.

    Anybody have any actionable fixes for this?

    Thanks

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

Viewing 1 replies (of 1 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    Here is some code you can add to your theme’s functions.php to accomplish this:

    add_filter( 'post_link', function( $url, $post ) {
    	static $recursing = false;
    	if ( $recursing ) {
    		return $url;
    	}
    	$recursing = true;
    	if ( ! function_exists( 'post_supports_amp' ) || ! post_supports_amp( $post ) ) {
    		return $url;
    	}
    	if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
    		$url = amp_get_permalink( $post->ID );
    	}
    	$recursing = false;
    	return $url;
    }, 10, 2 );

    This won’t apply to links hard-coded inside of the post content.

Viewing 1 replies (of 1 total)
  • The topic ‘Forcing AMP – Internal Links on AMP Versions’ is closed to new replies.