Optimizing page content. Add nofollow wordpress.
As you know, links on sites can transfer the weight of pages and the site as a whole. End-to-end links pass less weight, and those that on separate pages convey more weight, but only from one page. Adding an attribute nofollow according to google, it should prohibit search robots from passing page weight through external links.
In this tutorial, I propose to consider a code snippet for closing all links., which are in the body of the article from indexing using the attribute nofollow. Topic WordPress the template, in the folder of the template itself there is a file functions.php.
Add the following code snippet to the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
add_filter('the_content', 'my_nofollow'); add_filter('the_excerpt', 'my_nofollow'); function my_nofollow($content) { return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content); } function my_nofollow_callback($matches) { $link = $matches[0]; $site_link = get_bloginfo('url'); if (strpos($link, 'rel') === false) { $link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link); } elseif (preg_match("%href=\S(?!$site_link)%i", $link)) { $link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link); } return $link; } |
Code checked, everything works perfectly. Good luck with development.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.