Automatically Redirect any 404 page to a Similar Post based on the Title Post Type & Taxonomy using 301 or 302 Redirects!
Welcome to WP 404 Auto Redirect to Similar Post!
This plugin automatically redirect 404 pages to similar posts based on Title, Post Types & Taxonomies. If nothing similar is found, visitors will be redirected to the homepage or a custom URL.
WP 404 Auto Redirect to Similar Post 1.0 introduces the concept of engines and groups which let you customize your own searching & matching logic. The plugin comes with 5 engines and 1 default group out of the box!
Default Group Engines:
Fix URL
Find and fix common URL mistakes.
Direct Match
Search for a Post that perfectly match keywords.
Search Post
Search for a similar Post.
Search Term
Search for a similar Term.
Search Post: Fallback
If a Post Type is set in the WP Query, redirect to the Post Type Archive.
WP 404 Auto Redirect to Similar Post is 100% compatible with all popular manual redirection plugins:
If you use one of them, but missed a manual redirection and a 404 is about to be displayed, WP 404 Auto Redirect to Similar Post will cover you.
They talk about us! 🙂
/wp-content/plugins/wp-404-auto-redirect-similar-post
directory, or install the plugin through the WordPress plugins screen directly.Advanced Usage: If you don’t know how to use filters & actions, please read the official WordPress Plugin API.
// Create a Group with only 3 Default Engines, and set a custom fire sequence add_action('wp404arsp/search/init', 'my_404_group'); function my_404_group($query){ wp404arsp_register_group(array( // Set Group Name 'name' => 'My Group', // Set Group Slug 'slug' => 'my_group', // Set Engines & the fire sequence 'engines' => array( 'default_post', // Add Default: Search Post Engine 'default_fix_url', // Add Default: Fix URL Engine 'default_direct', // Add Default: Default: Direct Match Engine ) )); } // Trigger the Custom Group: 'My Group' when the 404 Page URL starts with '/product/xxxx/' add_filter('wp404arsp/search/group', 'my_404_group_trigger', 10, 2); function my_404_group_trigger($group, $query){ // Developers: Print $query array for more request context // Our condition: 404 Page URL starts with '/product/xxxx/' if(preg_match('#/product/(.+?)/?$#i', $query['request']['url'])){ $group = 'my_group'; // My Group Slug } // Always return Group return $group; }
Advanced Usage: If you don’t know how to use filters & actions, please read the official WordPress Plugin API.
// Create a Custom Engine add_action('wp404arsp/search/init', 'my_404_group_engine'); function my_404_group_engine($query){ wp404arsp_register_engine(array( // Set Engine Name 'name' => 'My Engine', // Set Engine Slug 'slug' => 'my_engine', // Set Engine Weight (Score = Keyword_Found * Weight) 'weight' => 100, // Set Primary Option (true|false). If Primary is true, then stop fire sequence if the score > 0. 'primary' => true )); // Use the engine in a new Group called 'My Group' wp404arsp_register_group(array( // Set Group Name 'name' => 'My Group', // Set Group Slug 'slug' => 'my_group', // Set Engines & the fire sequence 'engines' => array( 'my_engine', // Add My Engine ) )); } // Trigger the Custom Group: 'My Group' when the 404 Page URL starts with '/product/xxxx/' add_filter('wp404arsp/search/group', 'my_404_group_trigger', 10, 2); function my_404_group_trigger($group, $query){ // Developers: Print $query array for more request context // Our condition: 404 Page URL starts with '/product/xxxx/' if(preg_match('#/product/(.+?)/?$#i', $query['request']['url'])){ $group = 'my_group'; // My Group Slug } // Always return Group return $group; } // Define a Custom Engine Logic add_filter('wp404arsp/search/engine/my_engine', 'my_404_engine_definition', 10, 3); function my_404_engine_definition($result, $query, $group){ // Developers: Print $query array for more request context // You have access to $query & the current $group as a context for the engine logic // In this example 'My Engine' is the only engine in 'My Group' // 'My Group' is triggered when the 404 Page URL starts with '/product/xxxx/' // What we want: Search for a similar post inside a specific Post Type: 'project' // Grab all Keywords in the URL $keywords = explode('-', $query['request']['keywords']['all']); // Run Search $search = wp404arsp_search(array( 'keywords' => $keywords, // Add keywords 'mode' => 'post', // Search for Post 'post_type' => 'project', // inside Post Type: 'project' ), $query); // Found something! if($search['score'] > 0){ // Return result return array( 'score' => $search['score'], 'url' => get_permalink($search['post_id']), 'why' => "This engine is Awesome! We found a similar Product inside the Post Type <strong>project</strong>!" ); } // Nothing found :( else{ return "Mehh... No similar Product found inside the Post Type <strong>project</strong>."; } }
Advanced Usage: If you don’t know how to use filters & actions, please read the official WordPress Plugin API.
add_action('wp404arsp/search/init', 'my_404_manipulate_groups_and_engines'); function my_404_manipulate_groups_and_engines($query){ // Move the default engine 'Direct Match' at the end of the 'Default Group' fire Sequence wp404arsp_reorder_group_engines(array( // Target Group Slug 'group' => 'default', // Target Engine Slug 'engine' => 'default_direct', // Set new Position in fire sequence. (In this example: 4 instead of 2). 'order' => 4 )); // Register new Engines & Fire Sequence for the existing Group 'My Group' wp404arsp_register_group_engines(array( // Target Group Slug 'group' => 'my_group', // New Engines & Fire Sequence 'engines' => array( 'my_engine', // Add Custom: My Engine 'default_post' // Add Default: Search Post Engine ) )); // Deregister an existing Engine. // The engine will be removed from any Groups which use it. The engine won't be registered anymore. // Target specific Engine Slug wp404arsp_deregister_engine('my_another_engine'); }
Advanced Usage: If you don’t know how to use filters & actions, please read the official WordPress Plugin API.
// Always trigger the Custom Group 'My Group' instead of the Default Group add_filter('wp404arsp/search/group', 'my_404_group_trigger_forever', 10, 2); function my_404_group_trigger_forever($group, $query){ // Developers: Print $query array for more request context // Always return 'My Group' return 'my_group'; }
Advanced Usage: If you don’t know how to use filters & actions, please read the official WordPress Plugin API.
// Do not load the plugin if the 404 URL starts with '/calendar/xxxx/' add_filter('wp404arsp/init', 'my_404_no_init', 10, 2); function my_404_no_init($init, $query){ // Developers: Print $query array for more request context if(preg_match('#/calendar/(.+?)/?$#i', $query['request']['url'])){ $init = false; } return $init; }
Advanced Usage: If you don’t know how to use filters & actions, please read the official WordPress Plugin API.
// Do something after a redirection add_action('wp404arsp/after_redirect', 'my_404_after_redirect'); function my_404_after_redirect($query){ // Developers: Print $query array for more request context // Send me an e-mail wp_mail( '[email protected]', 'WP 404 Auto Redirect: New redirection', 'Hi! New redirection from ' . $args['request']['url'] . ' to ' . $query['redirection']['url'], array('Content-Type: text/html; charset=UTF-8') ); return; }
action('wp404arsp/search/init', $query)
filter('wp404arsp/search/group', $group, $query)
filter('wp404arsp/search/query', $query)
filter('wp404arsp/search/engine/{engine}', $result, $query, $group)
filter('wp404arsp/search/results', $query)
filter('wp404arsp/search/redirect', $redirect, $query)
('wp404arsp/init', $init, $request_uri)
('wp404arsp/settings', $settings)
/wp-admin/
path (Preview Mode)('wp404arsp/settings', $settings)
('wp404arsp/redirect', $args, $settings)
('wp404arsp/after_redirect', $args, $settings)
wp404arsp_no_redirect = 1
from possible redirections.wp404arsp_no_redirect = 1
from possible redirections.