-
Notifications
You must be signed in to change notification settings - Fork 2
/
auto-nofollow.php
42 lines (34 loc) · 1.16 KB
/
auto-nofollow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
/**
* @package wpsmall
*/
/**
* Plugin Name: Auto Nofollow
* Plugin URI: https://github.com/themeasia/auto-nofollow
* Description: WordPress All post a tag all external link auto nofollow plugin
* Version: 1.0.1
* Author: ThemeAsia
* Author URI: https://themeasia.net
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: autonofollow
* Domain Path: /languages
*/
// If this file is called firectily, abort!!
defined( 'ABSPATH' ) or ( 'Hey, What are you doing here? you silly human!' );
function auto_nofollow($content) {
return preg_replace_callback('/<a[^>]+/', 'auto_nofollow_callback', $content);
}
function auto_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;
}
// Filter Hook
add_filter('the_content', 'auto_nofollow');
add_filter('the_excerpt', 'auto_nofollow');