Nofollow for external link - Version 1.2.2

Version Description

  • Fix the issue, rel="noopener" automatically added to link tag when "Open link in a new tab" checkbox checked in latest WordPress version
  • Replace blank index.php file with comment
Download this release

Release Info

Developer cybernetikz
Plugin Icon 128x128 Nofollow for external link
Version 1.2.2
Comparing to
See all releases

Code changes from version 1.2.1 to 1.2.2

css/index.php CHANGED
@@ -0,0 +1,2 @@
 
 
1
+ <?php
2
+ // Silence is golden.
images/index.php CHANGED
@@ -0,0 +1,2 @@
 
 
1
+ <?php
2
+ // Silence is golden.
index.php CHANGED
@@ -0,0 +1,2 @@
 
 
1
+ <?php
2
+ // Silence is golden.
nofollow-external-link.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Nofollow for external link
4
  Plugin URI: http://www.cybernetikz.com
5
  Description: Just simple, if you activate this plugins, <code>rel=&quot;nofollow&quot;</code> and <code>target=&quot;_blank&quot;</code> will be added automatically, for all the external links of your website <strong>posts</strong> or <strong>pages</strong>. Also you can <strong>exclude domains</strong>, not to add <code>rel=&quot;nofollow&quot;</code> for the selected external links.
6
- Version: 1.2.1
7
  Author: CyberNetikz
8
  Author URI: http://www.cybernetikz.com
9
  License: GPL2
@@ -18,7 +18,7 @@ function cn_nf_install() {
18
  register_activation_hook(__FILE__,'cn_nf_install');
19
 
20
  function cn_nf_uninstall() {
21
- delete_option( 'cn_nf_apply_to_menu' );
22
  }
23
  register_deactivation_hook(__FILE__,'cn_nf_uninstall');
24
 
@@ -42,17 +42,15 @@ function cn_nf_admin_sidebar() {
42
  ),
43
  );
44
  //shuffle( $banners );
45
- ?>
46
- <div class="cn_admin_banner">
47
- <?php
48
  $i = 0;
 
49
  foreach ( $banners as $banner ) {
50
  echo '<a target="_blank" href="' . esc_url( $banner['url'] ) . '"><img width="261" height="190" src="' . plugins_url( 'images/' . $banner['img'], __FILE__ ) . '" alt="' . esc_attr( $banner['alt'] ) . '"/></a><br/><br/>';
51
  $i ++;
52
  }
53
- ?>
54
- </div>
55
- <?php
56
  }
57
 
58
  function cn_nf_admin_style() {
@@ -76,113 +74,169 @@ add_action( 'admin_menu', 'cn_nf_plugin_menu');
76
  function cn_nf_option_page_fn() {
77
  $cn_nf_exclude_domains = get_option('cn_nf_exclude_domains');
78
  $cn_nf_apply_to_menu = get_option('cn_nf_apply_to_menu');
79
- ?>
80
- <div class="wrap">
81
- <h2>Nofollow for external link Options</h2>
82
- <div class="content_wrapper">
83
- <div class="left">
84
- <form method="post" action="options.php" enctype="multipart/form-data">
85
- <?php settings_fields( 'cn-nf-settings-group' ); ?>
86
- <table class="form-table">
87
-
88
- <tr valign="top">
89
- <th scope="row">Apply nofollow to Menu</th>
90
- <td><input <?php echo ($cn_nf_apply_to_menu == 1)?'checked="checked"':''; ?> type="checkbox" name="cn_nf_apply_to_menu" id="cn_nf_apply_to_menu" value="1" /><br />
91
- <em>If you check this box then <code>rel="nofollow"</code> and <code>target="_blank"</code> will be added to all external links of your <a href="nav-menus.php">Theme Menus</a></em></td>
92
- </tr>
93
-
94
- <tr valign="top">
95
- <th scope="row">Exclude Domains</th>
96
- <td><textarea name="cn_nf_exclude_domains" id="cn_nf_exclude_domains" class="large-text" placeholder="mydomain.com, my-domain.org, another-domain.net"><?php echo $cn_nf_exclude_domains?></textarea>
97
- <br /><em>Domain name <strong>must be</strong> comma(,) separated. <!--<br />Example: facebook.com, google.com, youtube.com-->Don't need to add <code>http://</code> or <code>https://</code><br /><code>rel="nofollow"</code> will not added to "Exclude Domains"</em></td>
98
- </tr>
99
- </table>
100
- <p class="submit">
101
- <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
102
- </p>
103
- </form>
104
- </div>
105
- <div class="right">
106
- <?php cn_nf_admin_sidebar(); ?>
107
- </div>
108
- </div>
109
- </div>
110
- <?php
111
- }
112
-
113
- function cn_nf_url_parse( $content ) {
114
 
115
  $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
 
116
  if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
117
- if( !empty($matches) ) {
118
-
119
- //$ownDomain = get_option('home');
120
- $ownDomain = $_SERVER['HTTP_HOST'];
121
-
122
- $exclude_domains_list = array();
123
- if(get_option('cn_nf_exclude_domains')!='') {
124
- $exclude_domains_list = explode(",",get_option('cn_nf_exclude_domains'));
125
- }
126
-
127
- for ($i=0; $i < count($matches); $i++)
128
- {
129
-
130
- $tag = $matches[$i][0];
131
- $tag2 = $matches[$i][0];
132
- $url = $matches[$i][0];
133
-
134
- // bypass #more type internal link
135
- $res = preg_match('/href(\s)*=(\s)*"[#|\/]*[a-zA-Z0-9-_\/]+"/',$url);
136
- if($res) {
137
- continue;
138
- }
139
-
140
- $pos = strpos($url,$ownDomain);
141
- if ($pos === false) {
142
-
143
- $domainCheckFlag = true;
144
-
145
- if(count($exclude_domains_list)>0) {
146
- $exclude_domains_list = array_filter($exclude_domains_list);
147
- foreach($exclude_domains_list as $domain) {
148
- $domain = trim($domain);
149
- if($domain!='') {
150
- $domainCheck = strpos($url,$domain);
151
- if($domainCheck === false) {
152
- continue;
153
- } else {
154
- $domainCheckFlag = false;
155
- break;
156
- }
157
- }
158
- }
159
- }
160
-
161
- $noFollow = '';
162
-
163
- // add target=_blank to url
164
- $pattern = '/target\s*=\s*"\s*_(blank|parent|self|top)\s*"/';
165
- preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
166
- if( count($match) < 1 )
167
- $noFollow .= ' target="_blank"';
168
-
169
- //exclude domain or add nofollow
170
- if($domainCheckFlag) {
171
- $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
172
- preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
173
- if( count($match) < 1 )
174
- $noFollow .= ' rel="nofollow"';
175
- }
176
-
177
- // add nofollow/target attr to url
178
- $tag = rtrim ($tag,'>');
179
- $tag .= $noFollow.'>';
180
- $content = str_replace($tag2,$tag,$content);
181
- }
182
- }
183
  }
184
  }
185
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  $content = str_replace(']]>', ']]&gt;', $content);
187
  return $content;
188
  }
@@ -191,4 +245,13 @@ add_filter( 'the_content', 'cn_nf_url_parse');
191
 
192
  if( get_option('cn_nf_apply_to_menu') ) {
193
  add_filter( 'wp_nav_menu_items', 'cn_nf_url_parse' );
194
- }
 
 
 
 
 
 
 
 
 
3
  Plugin Name: Nofollow for external link
4
  Plugin URI: http://www.cybernetikz.com
5
  Description: Just simple, if you activate this plugins, <code>rel=&quot;nofollow&quot;</code> and <code>target=&quot;_blank&quot;</code> will be added automatically, for all the external links of your website <strong>posts</strong> or <strong>pages</strong>. Also you can <strong>exclude domains</strong>, not to add <code>rel=&quot;nofollow&quot;</code> for the selected external links.
6
+ Version: 1.2.2
7
  Author: CyberNetikz
8
  Author URI: http://www.cybernetikz.com
9
  License: GPL2
18
  register_activation_hook(__FILE__,'cn_nf_install');
19
 
20
  function cn_nf_uninstall() {
21
+ delete_option( 'cn_nf_apply_to_menu' );
22
  }
23
  register_deactivation_hook(__FILE__,'cn_nf_uninstall');
24
 
42
  ),
43
  );
44
  //shuffle( $banners );
45
+
 
 
46
  $i = 0;
47
+ echo '<div class="cn_admin_banner">';
48
  foreach ( $banners as $banner ) {
49
  echo '<a target="_blank" href="' . esc_url( $banner['url'] ) . '"><img width="261" height="190" src="' . plugins_url( 'images/' . $banner['img'], __FILE__ ) . '" alt="' . esc_attr( $banner['alt'] ) . '"/></a><br/><br/>';
50
  $i ++;
51
  }
52
+ echo '</div>';
53
+
 
54
  }
55
 
56
  function cn_nf_admin_style() {
74
  function cn_nf_option_page_fn() {
75
  $cn_nf_exclude_domains = get_option('cn_nf_exclude_domains');
76
  $cn_nf_apply_to_menu = get_option('cn_nf_apply_to_menu');
77
+ include_once ('nofollow-option-page.php');
78
+ }
79
+
80
+ function cn_nf_is_internal_link($url)
81
+ {
82
+ // bypass #more type internal link
83
+ $result = preg_match('/href(\s)*=(\s)*"[#|\/]*[a-zA-Z0-9-_\/]+"/', $url);
84
+
85
+ if ($result) {
86
+ return true;
87
+ }
88
+
89
+ $pos = strpos($url, cn_nf_get_domain());
90
+ if ($pos !== false) {
91
+ return true;
92
+ }
93
+
94
+ return false;
95
+ }
96
+
97
+ function cn_nf_is_link_available($content='')
98
+ {
99
+ if ($content=='') {
100
+ return null;
101
+ }
 
 
 
 
 
 
 
 
 
 
102
 
103
  $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
104
+
105
  if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
106
+ return $matches;
107
+ }
108
+
109
+ return null;
110
+ }
111
+
112
+ function cn_nf_get_domain()
113
+ {
114
+ // return get_option('home');
115
+ return $_SERVER['HTTP_HOST'];
116
+ }
117
+
118
+ function cn_nf_get_exclude_domains_list()
119
+ {
120
+ $exclude_domains_list = array();
121
+
122
+ if(get_option('cn_nf_exclude_domains')!='') {
123
+ $exclude_domains_list = explode(",", get_option('cn_nf_exclude_domains'));
124
+ }
125
+
126
+ return $exclude_domains_list;
127
+ }
128
+
129
+ function cn_nf_is_domain_not_excluded($url)
130
+ {
131
+ $domain_check_flag = true;
132
+
133
+ $exclude_domains_list = cn_nf_get_exclude_domains_list();
134
+
135
+ if(!count($exclude_domains_list)) {
136
+ return $domain_check_flag;
137
+ }
138
+
139
+ $exclude_domains_list = array_filter($exclude_domains_list);
140
+
141
+ foreach($exclude_domains_list as $domain) {
142
+
143
+ $domain = trim($domain);
144
+
145
+ if($domain=='') {
146
+ continue;
147
+ }
148
+
149
+ $pos = strpos($url, $domain);
150
+
151
+ if($pos === false) {
152
+ continue;
153
+ } else {
154
+ $domain_check_flag = false;
155
+ break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  }
157
  }
158
+
159
+ return $domain_check_flag;
160
+ }
161
+
162
+ function cn_nf_add_target_blank($url, $tag)
163
+ {
164
+ $no_follow = '';
165
+ $pattern = '/target\s*=\s*"\s*_(blank|parent|self|top)\s*"/';
166
+
167
+ if (preg_match($pattern, $url) === 0) {
168
+ $no_follow .= ' target="_blank"';
169
+ }
170
+
171
+ if ($no_follow) {
172
+ $tag = cn_nf_update_close_tag($tag, $no_follow);
173
+ }
174
+
175
+ return $tag;
176
+ }
177
+
178
+ function cn_nf_add_rel_nofollow($url, $tag)
179
+ {
180
+ $no_follow = '';
181
+ // $pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
182
+ // $pattern = '/rel\s*=\s*\"[a-zA-Z0-9_\s]*[n|d]ofollow[a-zA-Z0-9_\s]*\"/';
183
+ $pattern = '/rel\s*=\s*\"[a-zA-Z0-9_\s]*\"/';
184
+
185
+ $result = preg_match($pattern, $url, $match);
186
+
187
+ if ($result === 0) {
188
+ $no_follow .= ' rel="nofollow"';
189
+ } else {
190
+ if (strpos($match[0], 'nofollow') === false &&
191
+ strpos($match[0], 'dofollow') === false) {
192
+ $temp = $match[0];
193
+ $temp = substr_replace($temp, ' nofollow"', -1);
194
+ $tag = str_replace($match[0], $temp, $tag);
195
+ }
196
+ }
197
+
198
+ if ($no_follow) {
199
+ $tag = cn_nf_update_close_tag($tag, $no_follow);
200
+ }
201
+
202
+ return $tag;
203
+ }
204
+
205
+ function cn_nf_update_close_tag($tag, $no_follow)
206
+ {
207
+ return substr_replace($tag, $no_follow.'>', -1);
208
+ }
209
+
210
+ function cn_nf_url_parse( $content )
211
+ {
212
+
213
+ $matches = cn_nf_is_link_available($content);
214
+
215
+ if ($matches === null) {
216
+ return $content;
217
+ }
218
+
219
+ // loop through each links
220
+ for ($i=0; $i < count($matches); $i++)
221
+ {
222
+ $tag = $matches[$i][0];
223
+ $url = $matches[$i][0];
224
+
225
+ if(cn_nf_is_internal_link($url)) {
226
+ continue;
227
+ }
228
+
229
+ $tag = cn_nf_add_target_blank($url, $tag);
230
+
231
+ //exclude domain or add nofollow
232
+ if(cn_nf_is_domain_not_excluded($url)) {
233
+ $tag = cn_nf_add_rel_nofollow($url, $tag);
234
+ }
235
+
236
+ $content = str_replace($url, $tag, $content);
237
+
238
+ } // end for loop
239
+
240
  $content = str_replace(']]>', ']]&gt;', $content);
241
  return $content;
242
  }
245
 
246
  if( get_option('cn_nf_apply_to_menu') ) {
247
  add_filter( 'wp_nav_menu_items', 'cn_nf_url_parse' );
248
+ }
249
+
250
+ /*function dd($value)
251
+ {
252
+ if (is_array($value)) {
253
+ die(print_r($value));
254
+ } else {
255
+ die(htmlentities($value));
256
+ }
257
+ }*/
nofollow-option-page.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div class="wrap">
2
+ <h2>Nofollow for external link Options</h2>
3
+ <div class="content_wrapper">
4
+ <div class="left">
5
+ <form method="post" action="options.php" enctype="multipart/form-data">
6
+ <?php settings_fields( 'cn-nf-settings-group' ); ?>
7
+ <table class="form-table">
8
+
9
+ <tr valign="top">
10
+ <th scope="row">Apply nofollow to Menu</th>
11
+ <td><input <?php echo ($cn_nf_apply_to_menu == 1)?'checked="checked"':''; ?> type="checkbox" name="cn_nf_apply_to_menu" id="cn_nf_apply_to_menu" value="1" /><br />
12
+ <em>If you check this box then <code>rel="nofollow"</code> and <code>target="_blank"</code> will be added to all external links of your <a href="nav-menus.php">Theme Menus</a></em></td>
13
+ </tr>
14
+
15
+ <tr valign="top">
16
+ <th scope="row">Exclude Domains</th>
17
+ <td><textarea name="cn_nf_exclude_domains" id="cn_nf_exclude_domains" class="large-text" placeholder="mydomain.com, my-domain.org, another-domain.net"><?php echo $cn_nf_exclude_domains?></textarea>
18
+ <br /><em>Domain name <strong>must be</strong> comma(,) separated. <!--<br />Example: facebook.com, google.com, youtube.com-->Don't need to add <code>http://</code> or <code>https://</code><br /><code>rel="nofollow"</code> will not added to "Exclude Domains"</em></td>
19
+ </tr>
20
+ </table>
21
+ <p class="submit">
22
+ <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
23
+ </p>
24
+ </form>
25
+ </div>
26
+ <div class="right">
27
+ <?php cn_nf_admin_sidebar(); ?>
28
+ </div>
29
+ </div>
30
+ </div>
readme.txt CHANGED
@@ -1,18 +1,18 @@
1
  === Nofollow for external link ===
2
  Plugin URI: http://www.cybernetikz.com
3
  Contributors: cybernetikz
4
- Donate link:
5
  Tags: nofollow,link,rel=nofollow,rel nofollow,seo,nofollow links,external link,external links,nofollow for external link,nofollow external link,nofollow external links
6
  Requires at least: 2.8.6
7
- Tested up to: 4.7.5
8
- Stable tag: 1.2.1
9
  License: GPL2
10
 
11
  Automatically insert `rel=nofollow` and `target=_blank` to all the external links into your website posts, pages or menus. Support exclude domain.
12
 
13
  == Description ==
14
 
15
- Just simple, if you use this plugin, `rel=nofollow` and `target=_blank` will be inserted automatically, for all the external links of your website posts, pages or theme menus.
16
 
17
  Also you can set <strong>exclude domains</strong>, not to add `rel=nofollow` for the selected external domain links.
18
 
@@ -55,6 +55,10 @@ Ans: Yes it does.
55
 
56
  == Changelog ==
57
 
 
 
 
 
58
  = 1.2.1 =
59
  * Fix issue with target="_self", target="_top", target="_parent"
60
 
@@ -77,7 +81,7 @@ Ans: Yes it does.
77
 
78
  = 1.0 =
79
  * First released version.
80
- * Please send me your feedback.
81
 
82
  == Upgrade Notice ==
83
  * First released version.
1
  === Nofollow for external link ===
2
  Plugin URI: http://www.cybernetikz.com
3
  Contributors: cybernetikz
4
+ Donate link:
5
  Tags: nofollow,link,rel=nofollow,rel nofollow,seo,nofollow links,external link,external links,nofollow for external link,nofollow external link,nofollow external links
6
  Requires at least: 2.8.6
7
+ Tested up to: 4.9
8
+ Stable tag: 1.2.2
9
  License: GPL2
10
 
11
  Automatically insert `rel=nofollow` and `target=_blank` to all the external links into your website posts, pages or menus. Support exclude domain.
12
 
13
  == Description ==
14
 
15
+ Just simple, if you use this plugin, `rel=nofollow` and `target=_blank` will be inserted automatically, for all the external links of your website posts, pages or theme menus.
16
 
17
  Also you can set <strong>exclude domains</strong>, not to add `rel=nofollow` for the selected external domain links.
18
 
55
 
56
  == Changelog ==
57
 
58
+ = 1.2.2 =
59
+ * Fix the issue, rel="noopener" automatically added to link tag when "Open link in a new tab" checkbox checked in latest WordPress version
60
+ * Replace blank index.php file with comment
61
+
62
  = 1.2.1 =
63
  * Fix issue with target="_self", target="_top", target="_parent"
64
 
81
 
82
  = 1.0 =
83
  * First released version.
84
+ * Please send me your feedback.
85
 
86
  == Upgrade Notice ==
87
  * First released version.