Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code snippets for the Yoast SEO canonical output
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Add Trailing Slash to Home Canonical - Yoast SEO
* Credit: Unknown
* Last Tested: Unknown
*/
add_filter( 'wpseo_canonical', 'yoast_seo_canonical_change_home_url_slash' );
function yoast_seo_canonical_change_home_url_slash( $canonical_url ) {
return trailingslashit( $canonical_url );
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Change the canonical link for the shop page
* Credit: Scott Weiss of somethumb.com
* Last Tested: Jan 25 2017 using Yoast SEO 6.0 on WordPress 4.9.1
*/
function yoast_seo_canonical_change_woocom_shop( $canonical ) {
if ( !is_shop() ) {
return $canonical;
}
return get_permalink( woocommerce_get_page_id( 'shop' ) );
}
add_filter( 'wpseo_canonical', 'yoast_seo_canonical_change_woocom_shop', 10, 1 );
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Yoast SEO Canonical From All Pages
* Credit: Yoast Team
* Last Tested: Jun 16 2017 using Yoast SEO 4.9 on WordPress 4.8
*/
add_filter( 'wpseo_canonical', '__return_false' );
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Yoast SEO Canonical From Individual or Multiple Items
* Credit: Yoast Team
* Last Tested: Jun 16 2017 using Yoast SEO 4.9 on WordPress 4.8
*********
* DIFFERENT POST TYPES
* Post: Change 123456 to the post ID
* Page: Change is_single to is_page and 123456 to the page ID
* Custom Post Type: Change is_single to is_singular and 123456 to the 'post_type_slug'
Example: is_singular( 'cpt_slug' )
*********
* MULTIPLE ITEMS
* Multiple of the same type can use an array.
Example: is_single( array( 123456, 1234567, 12345678 ) )
* Multiple of different types can repeat the if statement
*/
add_filter( 'wpseo_canonical', 'yoast_remove_canonical_items' );
function yoast_remove_canonical_items( $canonical ) {
if ( is_single ( 123456 ) ) {
return false;
}
/* Use a second if statement here when needed */
return $canonical; /* Do not remove this line */
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Yoast SEO Canonical From Search Pages Only
* Credit: Yoast Team
* Last Tested: Jun 16 2017 using Yoast SEO 4.9 on WordPress 4.8
*/
add_filter( 'wpseo_canonical', 'yoast_remove_canonical_search' );
function yoast_remove_canonical_search( $canonical ) {
if( is_search() ) {
return false;
} else {
return $canonical;
}
}
@petervcook

This comment has been minimized.

Show comment
Hide comment
@petervcook

petervcook Aug 8, 2017

Where does one place this filter?

petervcook commented Aug 8, 2017

Where does one place this filter?

@amboutwe

This comment has been minimized.

Show comment
Hide comment
@amboutwe

amboutwe Aug 10, 2017

@petervcook The most common place is your theme's functions.php file. If you're new to using code snippets, this article is a great beginner guide: http://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/

Owner

amboutwe commented Aug 10, 2017

@petervcook The most common place is your theme's functions.php file. If you're new to using code snippets, this article is a great beginner guide: http://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/

@jgresalfi

This comment has been minimized.

Show comment
Hide comment
@jgresalfi

jgresalfi Sep 28, 2017

Thanks for posting these -- is there a way to clear canonicals based on whether or not they're on a standard post? I want to leave them on pages but remove from posts but can't find whether or not there's a "type" that identifies all standard WP posts.

jgresalfi commented Sep 28, 2017

Thanks for posting these -- is there a way to clear canonicals based on whether or not they're on a standard post? I want to leave them on pages but remove from posts but can't find whether or not there's a "type" that identifies all standard WP posts.

@amboutwe

This comment has been minimized.

Show comment
Hide comment
@amboutwe

amboutwe Sep 28, 2017

@jgresalfi is_single() without a post id should work for all posts while leaving pages alone.

Owner

amboutwe commented Sep 28, 2017

@jgresalfi is_single() without a post id should work for all posts while leaving pages alone.

@sebastianheinz

This comment has been minimized.

Show comment
Hide comment
@sebastianheinz

sebastianheinz Oct 3, 2017

Is there a way to set the canonical to the current page/language URL for self referencing?

sebastianheinz commented Oct 3, 2017

Is there a way to set the canonical to the current page/language URL for self referencing?

@amboutwe

This comment has been minimized.

Show comment
Hide comment
@amboutwe

amboutwe Oct 4, 2017

@sebastianheinz The default canonical is the permalink for the post/page.

Owner

amboutwe commented Oct 4, 2017

@sebastianheinz The default canonical is the permalink for the post/page.

@erichazann

This comment has been minimized.

Show comment
Hide comment
@erichazann

erichazann Mar 9, 2018

I think I have the same question as sebastianheinz. According to best practices for canonicals and hreflang attributes, the canonical should be set to the language/localized url, but Yoast uses the permalink of the page w/o any localization parameters. Is there a code sample to pass through the localization params? Example: www.example.com/article-title/?hl=es The Yoast canonical is www.example.com/article-title/, when I need it to be ww.example.com/article-title/?hl=es. Thanks!

erichazann commented Mar 9, 2018

I think I have the same question as sebastianheinz. According to best practices for canonicals and hreflang attributes, the canonical should be set to the language/localized url, but Yoast uses the permalink of the page w/o any localization parameters. Is there a code sample to pass through the localization params? Example: www.example.com/article-title/?hl=es The Yoast canonical is www.example.com/article-title/, when I need it to be ww.example.com/article-title/?hl=es. Thanks!

@shamsiaidin

This comment has been minimized.

Show comment
Hide comment
@shamsiaidin

shamsiaidin Jul 18, 2018

hi, 1-what is this codes do with Canonical ?
2- in product page we have to description , one that shown in description tab bottom of product and one is expert description which show front of product picture in single page of product, if we want to use description tab contents for snippet Search Appearance what variable must be use?

shamsiaidin commented Jul 18, 2018

hi, 1-what is this codes do with Canonical ?
2- in product page we have to description , one that shown in description tab bottom of product and one is expert description which show front of product picture in single page of product, if we want to use description tab contents for snippet Search Appearance what variable must be use?

@ashki1

This comment has been minimized.

Show comment
Hide comment
@ashki1

ashki1 Sep 7, 2018

Hello there

I have a site like - https://mysite.com and I see the canonical name to be http://mysite.com . (note the http part here). Now I want it to be changed to https. Can anyone please guide me with that.

As of now this is what it looks like - in the source code

I want to use wpseo_canonical filter in WP and i'm stuck.

Any help is appreciated!

ashki1 commented Sep 7, 2018

Hello there

I have a site like - https://mysite.com and I see the canonical name to be http://mysite.com . (note the http part here). Now I want it to be changed to https. Can anyone please guide me with that.

As of now this is what it looks like - in the source code

I want to use wpseo_canonical filter in WP and i'm stuck.

Any help is appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment