I have a default theme. When i add media to wordpress site. By default it use simple "a href" tag I want to add attribute "rel=nofollow" to it. Can anybody please point me in right direction?.
I have a default theme. When i add media to wordpress site. By default it use simple "a href" tag I want to add attribute "rel=nofollow" to it. Can anybody please point me in right direction?.
Share Improve this question edited Nov 8, 2014 at 14:07 fuxia♦ 107k39 gold badges255 silver badges461 bronze badges asked Nov 8, 2014 at 13:06 HybridHybrid 1033 bronze badges 2- You have tagged this question with both wordpress and wordpress - is this a question about a self-hosted WP (and hence on-topic) or about a blog on wordpress (and hence not)? – Johannes P. Commented Nov 8, 2014 at 13:26
- Not a blog. Self hosted – Hybrid Commented Nov 8, 2014 at 13:27
2 Answers
Reset to default 1You need to hook into the image_send_to_editor filter. This allows you to modify the markup used when inserting an image into the content editor.
Something like this should work:
add_filter('image_send_to_editor', 'my_add_rel_nofollow', 10, 8);
function my_add_rel_nofollow($html, $id, $caption, $title, $align, $url, $size, $alt = '' ){
// check if there is already a rel value
if ( preg_match('/<a.*? rel=".*?">/', $html) ) {
$html = preg_replace('/(<a.*? rel=".*?)(".*?>)/', '$1 ' . 'nofollow' . '$2', $html);
} else {
$html = preg_replace('/(<a.*?)>/', '$1 rel="' . 'nofollow' . '" >', $html);
}
return $html;
}
This will first check if there is already a rel
attribute, and add the no_follow. Or, if there is no rel
attribute, it will add one, and set it to nofollow
.
This works for me. Not sure if its always been there pf if its part a nofollow plugin?