Hey @jsess79,
Do you mind explaining what you want to do in a little bit more detail? 🙂
We’ll be happy to provide you with a code snippet, but I’m do not fully understand yet what it is you’re trying to achieve.
Hey @arnaudbroes
I have many pages in my multisite network that I had provided with a canonical and a meta index at the time.
I would now like to switch these pages to noindex and remove the canonical that the combination of noindex and canonical on the original should not be used.
So the entire canonical tag in the metadata that is generated by AIOSEO should be removed.
Do you mean you only want to remove them if they have a custom canonical URL (which you can enter in our metabox under the Advanced settings) or simply whenever they have a canonical URL? Because they’ll always have one.
Yes, that is exactly what I mean. If I have a custom canonical url that I entered in the custom field under the advanced in your metabox. I would like to remove this on 500+ pages on my multisite … Therefore the question of a function to do this globally via a snippet in the multisite and not manually.
-
This reply was modified 5 months, 3 weeks ago by
jsess79.
@jsess79 I didn’t have a chance to test this code snippet yet, but I believe it should do the trick for you –
add_filter( 'aioseo_canonical_url', 'remove_custom_canonical_url' );
function remove_custom_canonical_url( $canonicalUrl ) {
$postId = get_the_ID();
if ( ! $postId ) {
return $canonicalUrl;
}
$metaData = aioseo()->meta->metaData->getMetaData( $postId );
if ( is_object( $metaData ) && ! empty( $metaData->canonical_url ) ) {
return '';
}
return $canonicalUrl;
}
Basically, you need to check the post record that we have in our custom table to see if it has a custom canonical URL. If so, you can empty it out.
This should remove the canonical URL for all posts that have a custom one. Keep in mind they will no longer output any canonical URL at all.
Hey @jsess79,
I’ll close this thread for now since I haven’t heard back from you yet, but please let me know in case you still need any help.
Hey @arnaudbroes
sorry for the late response. Unfortunately your code did not work.
Do you have any other ideas?
Best Jens
@jsess79 It should work so you’ll probably need to debug the snippet to see what’s going on your website. I’d verify what data the plugin is returning for $metaData
. var_dump
should do the trick.