Hello,
I apologize for the issue you are facing with the robots snippet code. It seems like you want to change nofollow
to follow
on specific URLs.
Based on the provided code snippets, it looks like the second code snippet you shared is adding additional meta tags along with the index
tag. This might be causing conflicts with the desired output.
To simplify and ensure that only the index
tag is added without any additional tags, you can modify the code snippet as follows:
add_filter( 'rank_math/frontend/robots', function( $robots ) {
$robots = array(); // Resetting the $robots array
$robots['index'] = 'index'; // Adding only the 'index' tag
return $robots;
});
By resetting the $robots
array before adding the index
tag, you can ensure that only the desired tag is included without any additional tags.
Please try using this modified code snippet and let us know if it resolves the issue you are facing. If you continue to experience difficulties, feel free to provide more details or reply to this ticket for further assistance.
Thank you for bringing this to our attention.
Sources:
How to Make Your Theme Rank Math Compatible » Rank Math
How to NoIndex All Posts in a Category » Rank Math
How to Use the Noydir Robots Meta Tag » Rank Math
Advanced Tab in Rank Math SEO’s Meta Box
It works on standard pages, but not where I need it. It’s an SEO friendly page generated by filtering plugin.
Since I can’t edit:
using
$robots[]="";
var_dump($robots);
$robots['index'] = 'index';
$robots['follow'] = 'follow';
result is:
array(3) {
["noindex"]=>
string(7) "noindex"
["nofollow"]=>
string(8) "nofollow"
[0]=>
string(0) ""
}
without var dump it’s modified, but lives a comma:
<meta name="robots" content=", index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large"/>
This works:
add_filter( 'rank_math/frontend/robots', function( $robots ) {
if ( strpos( $_SERVER['REQUEST_URI'], '/sklep/' ) !== false ) {
$robots['noindex'] = 'index';
$robots['nofollow'] = 'follow';
// var_dump($robots);
}
return $robots;
}, 9999 );
What’s interesting, when I remove priority, after change var_dump shows index and follow, but eventually it’s set to noindex,nofollow anyway. I suppose some script changes it.. i hope it doesn’t affect the performance etc?
Hello,
That filter code shouldn’t cause duplication of the meta tags.
To set a page that is already set to noindex
to index
simply assign the proper value to the available array keys.
Here’s a code we modified for you:
add_filter( 'rank_math/frontend/robots', function( $robots ) {
if ( strpos( $_SERVER['REQUEST_URI'], '/sklep/' ) !== false ) {
$robots['index'] = 'index';
$robots['follow'] = 'follow';
}
return $robots;
} );
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.