WP 404 Auto Redirect to Similar Post - Version 0.9.0.2

Version Description

  • Fix: Plugin priority set to 999 by debault
  • Added: New Filter available ('wp404arsp/init', $init, $request_uri)
  • Added: Filters & Actions documentation
  • Added: Hooks tab documentation in Administration panel
  • Improvement: Plugin's page description
  • Removed: Unnecessary filter ('wp404arsp/settings', $settings)
Download this release

Release Info

Developer hwk-fr
Plugin Icon 128x128 WP 404 Auto Redirect to Similar Post
Version 0.9.0.2
Comparing to
See all releases

Code changes from version 0.9.0.1 to 0.9.0.2

Files changed (2) hide show
  1. readme.txt +124 -8
  2. wp-404-auto-redirect-similar-post.php +121 -18
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: hwk-fr
3
  Donate link: https://hwk.fr/
4
  Tags: SEO, 404, Redirect, 301, Similar, Related, Search, Broken Link, Webmaster Tools, Google
5
  Requires at least: 4.0
6
- Tested up to: 4.9.1
7
- Stable tag: 0.9.0.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -14,20 +14,28 @@ Automatically Redirect any 404 to a Similar Post based on the Requested URL Titl
14
 
15
  Welcome to WP 404 Auto Redirect to Similar Post!
16
 
17
- This plugin automatically redirect all your 404 requests to similar posts based on Title & Post Types. If nothing similar is found, it will redirect to the homepage.
18
 
19
  = Features: =
20
 
21
  * Automatically detect any 404.
22
- * Automatically search a similar post based on the Title, Post Type, Category & Taxonomy.
23
- * If nothing similar is found, choose your Fallback Behavior: Redirection or Default 404.
24
- * Choose the redirection HTTP header status: 301 / 302.
 
 
 
 
 
 
 
 
25
  * Exclude one or multiple post types from possible redirections.
26
  * Exclude one or multiple taxonomies from possible redirections.
27
  * Exclude one or multiple posts based on a custom post meta.
28
  * Exclude one or multiple terms based on a custom term meta.
29
- * Administrators: Display the Debug Console instead of being redirected.
30
- * Preview possible results directly from the administration panel.
31
 
32
  = But Also: =
33
 
@@ -58,6 +66,106 @@ They talk about it! :)
58
  * [The Ultimate Guide to Starting a Travel Blog](https://www.littlemissgemtravels.com/ultimate-guide-to-starting-a-travel-blog/)
59
  * [80 Best WordPress Plugins for 2018](https://sayoho.com/80-best-wordpress-plugins-for-2018/)
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  == Installation ==
62
 
63
  = Wordpress Install =
@@ -77,6 +185,14 @@ They talk about it! :)
77
 
78
  == Changelog ==
79
 
 
 
 
 
 
 
 
 
80
  = 0.9.0.1 =
81
  * Fix: Paged request redirection "Uncaught Argument" error
82
 
3
  Donate link: https://hwk.fr/
4
  Tags: SEO, 404, Redirect, 301, Similar, Related, Search, Broken Link, Webmaster Tools, Google
5
  Requires at least: 4.0
6
+ Tested up to: 4.9.8
7
+ Stable tag: 0.9.0.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
14
 
15
  Welcome to WP 404 Auto Redirect to Similar Post!
16
 
17
+ This plugin automatically redirect all your 404 requests to similar posts based on Title, Post Types & Taxonomies. If nothing similar is found, it will redirect visitors to the homepage or a custom URL.
18
 
19
  = Features: =
20
 
21
  * Automatically detect any 404.
22
+ * Automatically search a similar post based on multiple factors:
23
+ * Title
24
+ * Potential Post Type
25
+ * Potential Taxonomy
26
+ * If nothing similar is found, set your Fallback Behavior:
27
+ * Redirect to homepage
28
+ * Redirect to a custom URL
29
+ * Display the default 404 page
30
+ * Choose the redirection HTTP header status:
31
+ * 301 headers
32
+ * 302 headers
33
  * Exclude one or multiple post types from possible redirections.
34
  * Exclude one or multiple taxonomies from possible redirections.
35
  * Exclude one or multiple posts based on a custom post meta.
36
  * Exclude one or multiple terms based on a custom term meta.
37
+ * Display the Debug Console instead of being redirected (Admin).
38
+ * Preview possible redirection from the administration panel.
39
 
40
  = But Also: =
41
 
66
  * [The Ultimate Guide to Starting a Travel Blog](https://www.littlemissgemtravels.com/ultimate-guide-to-starting-a-travel-blog/)
67
  * [80 Best WordPress Plugins for 2018](https://sayoho.com/80-best-wordpress-plugins-for-2018/)
68
 
69
+ == Frequently Asked Questions ==
70
+
71
+ = Hooks: Filters & Actions =
72
+
73
+ **Note:** If you don't know about filters & actions, please read the official [WordPress Plugin API](https://codex.wordpress.org/Plugin_API).
74
+
75
+ **Filter: wp404arsp/init**
76
+
77
+ This filter is applied at the initialization of the plugin, in the `template_redirect` action.
78
+
79
+ By default, `$init = true` which means that the plugin will take action when a 404 is about to be displayed. If is set to `false`, then le plugin will stop it's initialization.
80
+
81
+ The second argument: `$request_uri` is the requested URI sent to the browser.
82
+
83
+ Returning `$init` as a boolean is **mandatory**.
84
+
85
+ Usage example:
86
+ `
87
+ add_filter('wp404arsp/init', 'my_404_no_init', 10, 2);
88
+ function my_404_no_init($init, $request_uri){
89
+ if($request_uri == 'https://www.my-website.com/custom-page')
90
+ $init = false;
91
+
92
+ return $init;
93
+ }
94
+ `
95
+
96
+ **Filter: wp404arsp/redirect**
97
+
98
+ This filter is applied right before the plugin sends a redirection. You can use it to set a custom behavior depending on multiple factors.
99
+
100
+ The array's key `$args['url']` will be used as the redirection's URI.
101
+
102
+ If the key `$args['url']` is set to `false`, the plugin's fallback behavior will be bypassed and the default 404 page will be displayed.
103
+
104
+ The second argument: `$settings` is an array of the plugin's settings.
105
+
106
+ Returning an `$args` array is **mandatory**.
107
+
108
+ Args array structure:
109
+ `
110
+ Array(
111
+ [request] => Array(
112
+ [full] => /this-is-404-page
113
+ [dirname] =>
114
+ [filename] => this-is-404-page
115
+ [extension] =>
116
+ )
117
+
118
+ [preview] =>
119
+ [process] => Array(
120
+ [request] => /this-is-404-page
121
+ [keywords] => Array
122
+ (
123
+ [sanitized] => this-is-404-page
124
+ [explode] => Array
125
+ (
126
+ [0] => this-is-404-page
127
+ )
128
+
129
+ )
130
+
131
+ )
132
+
133
+ [why] => Nothing similar found matching the requested URL.
134
+ [url] => https://www.my-website.com
135
+ )
136
+ `
137
+
138
+ Usage example:
139
+ `
140
+ add_filter('wp404arsp/redirect', 'my_404_redirect', 10, 2);
141
+ function my_404_redirect($args, $settings){
142
+ if($args['request']['full'] == '/this-is-404-page')
143
+ $args['url'] = 'https://www.my-website.com/an-another-fallback-page';
144
+
145
+ return $args;
146
+ }
147
+ `
148
+
149
+ **Action: wp404arsp/after_redirect**
150
+
151
+ This action is called right after a redirection is sent to the browser. You can use it to perform custom actions.
152
+
153
+ Usage example:
154
+ `
155
+ add_action('wp404arsp/after_redirect', 'my_404_after_redirect', 10, 2);
156
+ function my_404_after_redirect($args, $settings){
157
+ // Send me an e-mail
158
+ wp_mail(
159
+ 'my@email.com',
160
+ 'WP 404 Auto Redirect: New redirection',
161
+ 'Hi! New redirection from ' . $args['request']['full'] . ' to ' . $args['url'],
162
+ array('Content-Type: text/html; charset=UTF-8')
163
+ );
164
+
165
+ return;
166
+ }
167
+ `
168
+
169
  == Installation ==
170
 
171
  = Wordpress Install =
185
 
186
  == Changelog ==
187
 
188
+ = 0.9.0.2 =
189
+ * Fix: Plugin priority set to 999 by debault
190
+ * Added: New Filter available `('wp404arsp/init', $init, $request_uri)`
191
+ * Added: Filters & Actions documentation
192
+ * Added: Hooks tab documentation in Administration panel
193
+ * Improvement: Plugin's page description
194
+ * Removed: Unnecessary filter `('wp404arsp/settings', $settings)`
195
+
196
  = 0.9.0.1 =
197
  * Fix: Paged request redirection "Uncaught Argument" error
198
 
wp-404-auto-redirect-similar-post.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: WP 404 Auto Redirect to Similar Post
4
  * Description: Automatically Redirect any 404 to a Similar Post based on the Title, Post Type, Category & Taxonomy using 301 Redirects!
5
  * Author: hwk-fr
6
- * Version: 0.9.0.1
7
  * Author URI: http://hwk.fr
8
  */
9
 
@@ -42,6 +42,7 @@ function wp404arsp_admin_page_html(){
42
  <a href="#post-types" class="nav-tab"><?php _e('Post Types', 'wp404-auto-redirect'); ?></a>
43
  <a href="#taxonomies" class="nav-tab"><?php _e('Taxonomies', 'wp404-auto-redirect'); ?></a>
44
  <a href="#preview" class="nav-tab"><?php _e('Preview', 'wp404-auto-redirect'); ?></a>
 
45
  </h2>
46
 
47
  <div id="poststuff">
@@ -338,6 +339,104 @@ function wp404arsp_admin_page_html(){
338
 
339
  </div>
340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341
  </div>
342
 
343
  <div class="postbox">
@@ -402,9 +501,11 @@ function wp404arsp_init(){
402
 
403
  if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
404
  return;
405
-
406
 
407
- wp404arsp_request($_SERVER['REQUEST_URI']);
 
 
 
408
  }
409
 
410
  function wp404arsp_request($request, $preview = false){
@@ -431,7 +532,6 @@ function wp404arsp_request($request, $preview = false){
431
  $query['request']['filename'] = $path['filename'];
432
  $query['request']['extension'] = (!empty($path['extension'])) ? $path['extension'] : '';
433
 
434
-
435
  // Pagination found, try the URL without pagination
436
  if(!empty($query['request']['pagination']))
437
  return wp404arsp_redirect(
@@ -566,7 +666,16 @@ function wp404arsp_redirect($args, $settings = false){
566
  if(!$settings)
567
  $settings = wp404arsp_get_settings();
568
 
 
 
 
 
 
 
 
569
  $args = apply_filters('wp404arsp/redirect', $args, $settings);
 
 
570
 
571
  if(is_user_logged_in() && current_user_can('administrator') && ($settings['debug'] || (isset($args['preview']) && $args['preview'])))
572
  return wp404arsp_debug($args);
@@ -579,22 +688,15 @@ function wp404arsp_redirect($args, $settings = false){
579
  function wp404arsp_redirect_to($args, $settings){
580
  // Copy/paste from legacy WP_Redirect function()
581
  // File: wp-includes/pluggable.php
582
- // Added: WP404ARSP header & exit;
583
 
584
- global $is_IIS;
585
-
586
- $status = $settings['method'];
587
- $fallback = $settings['fallback']['type'] != 'disabled' ? $settings['fallback']['url'] : false;
588
-
589
- if((!isset($args['url']) || empty($args['url'])) && !$fallback)
590
- return;
591
 
592
- $location = $fallback;
593
 
594
- if(isset($args['url']) && !empty($args['url']))
595
- $location = $args['url'];
596
-
597
- $location = apply_filters('wp_redirect', $location, $status);
598
  $status = apply_filters('wp_redirect_status', $status, $location);
599
 
600
  if(!$location)
@@ -772,6 +874,7 @@ function wp404arsp_get_settings(){
772
  'debug' => null,
773
  'logs' => null,
774
  'method' => 301,
 
775
  'fallback' => array(
776
  'type' => 'home',
777
  'url' => home_url(),
@@ -798,7 +901,7 @@ function wp404arsp_get_settings(){
798
  if(((int)$return['method'] != 301) && ((int)$return['method'] != 302))
799
  $return['method'] = 301;
800
 
801
- return apply_filters('wp404arsp/settings', $return);
802
  }
803
 
804
  function wp404arsp_wp_parse_args_recursive(&$a, $b){
3
  * Plugin Name: WP 404 Auto Redirect to Similar Post
4
  * Description: Automatically Redirect any 404 to a Similar Post based on the Title, Post Type, Category & Taxonomy using 301 Redirects!
5
  * Author: hwk-fr
6
+ * Version: 0.9.0.2
7
  * Author URI: http://hwk.fr
8
  */
9
 
42
  <a href="#post-types" class="nav-tab"><?php _e('Post Types', 'wp404-auto-redirect'); ?></a>
43
  <a href="#taxonomies" class="nav-tab"><?php _e('Taxonomies', 'wp404-auto-redirect'); ?></a>
44
  <a href="#preview" class="nav-tab"><?php _e('Preview', 'wp404-auto-redirect'); ?></a>
45
+ <a href="#hooks" class="nav-tab"><?php _e('Hooks', 'wp404-auto-redirect'); ?></a>
46
  </h2>
47
 
48
  <div id="poststuff">
339
 
340
  </div>
341
 
342
+ <!-- Tab: Hooks -->
343
+ <div class="nav-tab-panel" id="hooks">
344
+
345
+ <div class="postbox">
346
+ <div class="inside">
347
+ <p><strong>Note:</strong> If you don't know about filters & actions, please read the official <a href="https://codex.wordpress.org/Plugin_API" target="_blank">WordPress Plugin API</a>.</p>
348
+
349
+ <h3>Filter: wp404arsp/init</h3>
350
+
351
+ <p>This filter is applied at the initialization of the plugin, in the <code>template_redirect</code> action. By default, <code>$init = true</code> which means that the plugin will take action when a 404 is about to be displayed. If is set to <code>false</code>, then le plugin will stop it's initialization. The second argument: <code>$request_uri</code> is the requested URI sent to the browser.</p>
352
+
353
+ <p>Returning <code>$init</code> as a boolean is <strong>mandatory</strong>.</p>
354
+
355
+ <p><strong>Usage example:</strong></p>
356
+ <pre><code style="display:block; white-space:pre-wrap;">add_filter('wp404arsp/init', 'my_404_no_init', 10, 2);
357
+ function my_404_no_init($init, $request_uri){
358
+ if($request_uri == 'https://www.my-website.com/custom-page')
359
+ $init = false;
360
+
361
+ return $init;
362
+ }</code></pre>
363
+ </div>
364
+ </div>
365
+
366
+ <div class="postbox">
367
+ <div class="inside">
368
+
369
+ <h3>Filter: wp404arsp/redirect</h3>
370
+
371
+ <p>This filter is applied right before the plugin sends a redirection. You can use it to set a custom behavior depending on multiple factors. The array's key <code>$args['url']</code> will be used as the redirection's URI. If the key <code>$args['url']</code> is set to <code>false</code>, the plugin's fallback behavior will be bypassed and the default 404 page will be displayed.
372
+
373
+ <p>The second argument: <code>$settings</code> is an array of the plugin's settings.</p>
374
+
375
+ <p>Returning an <code>$args</code> array is <strong>mandatory</strong>.</p>
376
+
377
+ <p><strong>Args array structure:</strong></p>
378
+ <pre><code style="display:block; white-space:pre-wrap;">Array(
379
+ [request] => Array(
380
+ [full] => /this-is-404-page
381
+ [dirname] =>
382
+ [filename] => this-is-404-page
383
+ [extension] =>
384
+ )
385
+
386
+ [preview] =>
387
+ [process] => Array(
388
+ [request] => /this-is-404-page
389
+ [keywords] => Array
390
+ (
391
+ [sanitized] => this-is-404-page
392
+ [explode] => Array
393
+ (
394
+ [0] => this-is-404-page
395
+ )
396
+
397
+ )
398
+
399
+ )
400
+
401
+ [why] => Nothing similar found matching the requested URL.
402
+ [url] => https://www.my-website.com
403
+ )</code></pre>
404
+ <p><strong>Usage example:</strong></p>
405
+ <pre><code style="display:block; white-space:pre-wrap;">add_filter('wp404arsp/redirect', 'my_404_redirect', 10, 2);
406
+ function my_404_redirect($args, $settings){
407
+ if($args['request']['full'] == '/this-is-404-page')
408
+ $args['url'] = 'https://www.my-website.com/an-another-fallback-page';
409
+
410
+ return $args;
411
+ }</code></pre>
412
+ </div>
413
+ </div>
414
+
415
+ <div class="postbox">
416
+ <div class="inside">
417
+
418
+ <h3>Action: wp404arsp/after_redirect</h3>
419
+
420
+ <p>This action is called right after a redirection is sent to the browser. You can use it to perform custom actions.</p>
421
+
422
+ <p><strong>Usage example:</strong></p>
423
+ <pre><code style="display:block; white-space:pre-wrap;">add_action('wp404arsp/after_redirect', 'my_404_after_redirect', 10, 2);
424
+ function my_404_after_redirect($args, $settings){
425
+ // Send me an e-mail
426
+ wp_mail(
427
+ 'my@email.com',
428
+ 'WP 404 Auto Redirect: New redirection',
429
+ 'Hi! New redirection from ' . $args['request']['full'] . ' to ' . $args['url'],
430
+ array('Content-Type: text/html; charset=UTF-8')
431
+ );
432
+
433
+ return;
434
+ }</code></pre>
435
+ </div>
436
+ </div>
437
+
438
+ </div>
439
+
440
  </div>
441
 
442
  <div class="postbox">
501
 
502
  if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
503
  return;
 
504
 
505
+ if(!apply_filters('wp404arsp/init', true, $_SERVER['REQUEST_URI']))
506
+ return;
507
+
508
+ wp404arsp_request($_SERVER['REQUEST_URI']);
509
  }
510
 
511
  function wp404arsp_request($request, $preview = false){
532
  $query['request']['filename'] = $path['filename'];
533
  $query['request']['extension'] = (!empty($path['extension'])) ? $path['extension'] : '';
534
 
 
535
  // Pagination found, try the URL without pagination
536
  if(!empty($query['request']['pagination']))
537
  return wp404arsp_redirect(
666
  if(!$settings)
667
  $settings = wp404arsp_get_settings();
668
 
669
+ $fallback = $settings['fallback']['url'];
670
+ if($settings['fallback']['type'] == 'disabled')
671
+ $fallback = false;
672
+
673
+ if((!isset($args['url']) || empty($args['url'])))
674
+ $args['url'] = $fallback;
675
+
676
  $args = apply_filters('wp404arsp/redirect', $args, $settings);
677
+ if(!$args['url'])
678
+ return;
679
 
680
  if(is_user_logged_in() && current_user_can('administrator') && ($settings['debug'] || (isset($args['preview']) && $args['preview'])))
681
  return wp404arsp_debug($args);
688
  function wp404arsp_redirect_to($args, $settings){
689
  // Copy/paste from legacy WP_Redirect function()
690
  // File: wp-includes/pluggable.php
 
691
 
692
+ // Added: 'WP-404-Auto-Redirect: true' header
693
+ // Added: 'wp404arsp/after_redirect' action
694
+ // Added: PHP exit;
 
 
 
 
695
 
696
+ global $is_IIS;
697
 
698
+ $status = $settings['method'];
699
+ $location = apply_filters('wp_redirect', $args['url'], $status);
 
 
700
  $status = apply_filters('wp_redirect_status', $status, $location);
701
 
702
  if(!$location)
874
  'debug' => null,
875
  'logs' => null,
876
  'method' => 301,
877
+ 'priority' => 999,
878
  'fallback' => array(
879
  'type' => 'home',
880
  'url' => home_url(),
901
  if(((int)$return['method'] != 301) && ((int)$return['method'] != 302))
902
  $return['method'] = 301;
903
 
904
+ return $return;
905
  }
906
 
907
  function wp404arsp_wp_parse_args_recursive(&$a, $b){