• Hi!

    I am switching from Yoast to The SEO Framework on a site, but I have a function that I’m not sure how to replicate.

    Here is the function:

    function rel_canonical_with_custom_tag_override($canonical) {
        global $post;
        if( is_singular() ) {
          $canonicalProduct = get_field('master_product', $post->ID);
          if( $canonicalProduct && '' != $canonicalProduct ) {
              return get_the_permalink($canonicalProduct);
          }
        }
    
        return $canonical;
    
    }
    add_filter( 'wpseo_canonical', 'rel_canonical_with_custom_tag_override' );

    Basically it checks to see if the product is a singular product, and it has a custom field of “Master Product”, then it changes the canonical URL to point to that product.

    This is for preventing Google from seeing products that just contain slight differences as duplicate content.

    It works with Yoast, is there a similar filter I can use?

    Thanks!
    Shaun

    https://www.ads-software.com/plugins/autodescription/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hi Shaun,

    I believe this is what you’re looking for:

    function rel_canonical_with_custom_tag_override( $url = '', $id = 0 ) {
    
    	if ( is_singular() ) {
    		$canonicalProduct = get_field( 'master_product', $id );
    		if ( $canonicalProduct )
    			return esc_url( get_the_permalink( $canonicalProduct ) );
    	}
    
    	return $url;
    }
    add_filter( 'the_seo_framework_rel_canonical_output', 'rel_canonical_with_custom_tag_override', 10, 2 );

    The if ( $canonicalProduct && '' != $canonicalProduct ) check is basically double, so I made it simpler :).
    The SEO framework also supplies a correct product ID in the 2nd parameter, so that’s nice too :).
    The SEO Framework doesn’t escape the URL at that point, so you’ll have to do that too (I added it already in the example).

    I hope this helps! Have a great day!

    Thread Starter Shaun Robinson

    (@rubious)

    Perfect, thanks! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Filter for overriding canonical URLs?’ is closed to new replies.