Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter krystianergotopia

    (@krystianergotopia)

    The code in the referenced link is also applying to the canonical link, which is really bad for SEO as the AMP is saying it is its own canonical. How to prevent this?

    I am no programmer, maybe its working with sth like this?

    function my_the_content_filter($content)
    {
    if (function_exists(‘is_amp_endpoint’) && is_amp_endpoint()) {
    $patterns = array(
    //patterns
    );
    $replacements = array(
    //replacements
    );
    $content = preg_replace($patterns, $replacements, $content);
    }
    return $content;
    }
    add_filter(‘the_content’, ‘my_the_content_filter’);

    Thread Starter krystianergotopia

    (@krystianergotopia)

    This is doing the job, but maybe you can investigate an option to include it in next update?

    /* post link filter */
    add_filter( 'post_link', 'change_amp_url', 10, 2 );
    
    function change_amp_url( $url, $postobj ) {
        static $recursing = false;
        if ( $recursing ) {
            return $url;
        }
    
        $recursing = true;
        if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
            if ( function_exists( 'post_supports_amp' ) && post_supports_amp( $postobj ) ) {
                $url = amp_get_permalink( $postobj->ID );           
            }
        }
        $recursing = false;
        return $url;
    }
    
    /* content link filter */
    add_filter( 'the_content', 'change_amp_url_content' );
    
    function change_amp_url_content($content)
    {
        $dom = new DOMDocument();
        $dom->loadHTML($content);
    
        $tags = $dom->getElementsByTagName('a');
        foreach ($tags as $tag) {
            $link = $tag->getAttribute('href'); // original url
            $extralink = '';
            if(stristr($link,'#')) {
                $pagelinktemp = explode("#",$link);
                $pagelink = $pagelinktemp[0];
                $extralink = '#'.$pagelinktemp[1];
            } else {
                $pagelink = $link;
            }
            if($pagelink!="") {     
                $postid = url_to_postid($pagelink);
                $postobj = get_post($postid); // getting appropriate post object            
                if($postobj) {          
                    $newlink = change_amp_url( $pagelink, $postobj ); //new url
                }
                else {
                    $newlink = $link;
                }
            }
            else
            {
                $newlink = $link;
            }
            if($link != $newlink) // change if only links are different
            {
                $content = str_replace($link, $newlink.$extralink, $content);
            }
        }
        return $content;
    }
    
    /* override canonical link */
    add_filter( 'wpseo_canonical', 'amp_override_canonical' );
    
    function amp_override_canonical($url) {
        global $post;
    
        if ( substr(get_permalink($post->ID),-4)=="/amp" ) {    
            $url = substr(get_permalink($post->ID),0,-4);
        }
    
        return $url;
    }
    Plugin Author Weston Ruter

    (@westonruter)

    It would be better to use a sanitizer for this I think. You can track this issue in GitHub: https://github.com/Automattic/amp-wp/issues/1389

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Internal links to AMP version’ is closed to new replies.