-
-
Save amboutwe/2aa7dcc9a38986e11fac68c7306cc091 to your computer and use it in GitHub Desktop.
<?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 ); | |
} |
please pay attention to this @amboutwe I have added canonical links to all pages of my website. Let's take the home page as an example.
Let's say https://sample.com
is entered there.
See the image below.
When you go to the view page source on the home page of the website, it is shown as follows
<link rel="canonical" href="//sample.com/" class="yoast-seo-meta-tag" />
See the image below.
Despite this, when the PageSpeed Insights test is run in the Google search console, the following error is displayed in the SEO section.
Document does not have a valid rel=canonicalIs not an absolute URL (//sample.com/)
It is shown in the picture below.
How to solve this problem?
I think the problem is that the https:
part before the double slash //
of //sample.com
is not showing up in the view page source.
This is not the proper place to request support. That said, I've seen this before and it's not caused by Yoast SEO. You have something in your setup that converts absolute links (with the https) to relative links (without the https).
Please check out our extensive help section or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.
I was working on a site that forces www, but for some reason not every page had "www." in the canonical URL. Most of them did, but I found a few, including the home page, that didn't. We are running the latest Yoast SEO (20.13). I had to write this filter:
add_filter(
'wpseo_canonical',
function (string $canonical_url): string {
if ($canonical_url && !str_starts_with($canonical_url, WP_HOME)) {
$canonical_url =
preg_replace('#^https?://[\w.-]+#', WP_HOME, $canonical_url);
}
return $canonical_url;
},
);
Finally I solved this problem. The reason was "Automatic HTTPS Rewrites" in AMP plugin and cloudflare and "Automatic HTTPS Rewrites" in WProket plugin where cloudflare was connected.
After removing the AMP plugin and turning off "Automatic HTTPS Rewrites", the problem was solved. This AMP plugin was one of the newspaper theme plugins.
If you want to remove canonical tag from the requests with url parameters. You can use this.
function custom_remove_canonical_with_get_params( $canonical ) {
// Check if there are any GET parameters in the URL
if ( ! empty( $_GET ) ) {
// If there are GET parameters, return the current URL without any canonical tag
return false;
}
// If there are no GET parameters, return the original canonical URL
return $canonical;
}
add_filter( 'wpseo_canonical', 'custom_remove_canonical_with_get_params' );
@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.