Skip to content

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 *********/
/* Change the canonical link for the shop page
* Credit: Scott Weiss of somethumb.com
* Yoast Doc: https://developer.yoast.com/features/seo-tags/canonical-urls/api/
* Last Tested: Jan 25 2017 using Yoast SEO 6.0 on WordPress 4.9.1
*/
add_filter( 'wpseo_canonical', 'yoast_seo_canonical_change_woocom_shop', 10, 1 );
function yoast_seo_canonical_change_woocom_shop( $canonical ) {
if ( !is_shop() ) {
return $canonical;
}
return get_permalink( woocommerce_get_page_id( 'shop' ) );
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Yoast SEO Canonical From All Pages
* Credit: Yoast Team
* Yoast Doc: https://developer.yoast.com/features/seo-tags/canonical-urls/api/
* 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
* Yoast Doc: https://developer.yoast.com/features/seo-tags/canonical-urls/api/
* 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
* Yoast Doc: https://developer.yoast.com/features/seo-tags/canonical-urls/api/
* 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;
}
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Add trailing slash to all Yoast SEO canonicals
* Credit: Unknown
* Yoast Doc: https://developer.yoast.com/features/seo-tags/canonical-urls/api/
* Last Tested: Oct 25 2019 using Yoast SEO 12.3 on WordPress 5.2.4
*/
add_filter( 'wpseo_canonical', 'yoast_seo_canonical_slash_add' );
function yoast_seo_canonical_slash_add( $canonical_url ) {
return trailingslashit( $canonical_url );
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove trailing slash from all Yoast SEO canonicals
* Credit: Unknown
* Yoast Doc: https://developer.yoast.com/features/seo-tags/canonical-urls/api/
* Last Tested: Oct 25 2019 using Yoast SEO 12.3 on WordPress 5.2.4
*/
add_filter( 'wpseo_canonical', 'yoast_seo_canonical_slash_remove' );
function yoast_seo_canonical_slash_remove( $canonical_url ) {
return untrailingslashit( $canonical_url );
}
@amarilindra
Copy link

@amboutwe

Canonical links and rel="prev" and rel="next" are removed on pages with "noindex" meta tag. Is there any way to force add them?

@ghaydtner
Copy link

When I go to an URL like myblog.com/blog/mypost/?what-is-my-canonical that has a query string on it, the canonical will be set to myblog.com/blog/mypost/?what-is-my-canonical.
In my eyes, the best canonical here would be myblog.com/blog/mypost/.
Is there a way to get rid of query strings for canonicals for blog posts?

@Suny-ticket
Copy link

Can we remove canonicals for only one post type (any specific)?
I am creating custom canonicals but Yoast gives me another canonical also, I want to replace canonical for that post-type generated by Yoast, not for other post types & pages.

@amboutwe
Copy link
Author

amboutwe commented Jan 5, 2023

@Suny-ticket Yes, the remove_post.php is for a single post, but that can be adjusted to be wider (all content matching specific criteria) depending on your needs. These are just a few examples so you or your developer have a starting point for the customization.

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