Changeset 2586104
- Timestamp:
- 08/20/2021 05:06:27 PM (3 years ago)
- Location:
- one-search-result
- Files:
-
- 2 deleted
- 8 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
one-search-result/tags/1.1.0/one-search-result.php
r2539075 r2586104 3 3 * Plugin Name: One Search Result 4 4 * Description: Automatically send a user to the page or post if it's the only search result available. 5 * Version: 1. 0.35 * Version: 1. 6 6 * Author: Brad Parbs 7 7 * Author URI: https://bradparbs.com/ … … 13 13 */ 14 14 15 if ( ! defined( 'ABSPATH' ) ) { 16 exit; 17 } 15 add_action( 'template_redirect', __NAMESPACE__ . '\\one_search_result' ); 18 16 19 17 /** 20 18 * If there is one search result, redirect to it automatically. 21 19 */ 22 add_action( 23 'template_redirect', 24 function () { 25 // Only apply to the search results. 26 if ( ! is_search() ) { 27 return; 28 } 20 function one_search_result() { 21 // Only apply to the search results. 22 if ( ! is_search() ) { 23 return; 24 } 29 25 30 31 32 33 26 // Filter 'one_search_result_pre_check' if you need to short-circuit it in some cases. 27 if ( ! apply_filters( 'one_search_result_pre_check', true ) ) { 28 return; 29 } 34 30 35 31 global $wp_query; 36 32 37 38 39 40 33 // Make sure we have results before doing anything. 34 if ( ! isset( $wp_query->posts ) || ! isset( $wp_query->post_count ) ) { 35 return; 36 } 41 37 42 43 44 45 38 // If we only have one result, perfect! 39 if ( ! ( 1 === $wp_query->post_count && 1 === count( $wp_query->posts ) ) ) { 40 return; 41 } 46 42 47 48 49 50 51 43 // Grab the permalink and make sure we got it. 44 $dest = get_permalink( $wp_query->posts[0] ); 45 if ( ! $dest ) { 46 return; 47 } 52 48 53 54 wp_safe_redirect( $dest, 302);55 49 // Send it off! 50 ); 51 exit; 56 52 57 } 58 ); 53 } -
one-search-result/tags/1.1.0/readme.txt
r2539075 r2586104 1 1 === One Search Result === 2 Contributors: bradparbs 2 Contributors: bradparbs 3 3 Tags: search, content, results, redirects 4 4 Requires at least: 5.2 5 Tested up to: 5. 7.26 Stable tag: 1. 0.35 Tested up to: 5. 6 Stable tag: 1. 7 7 License: GPLv2 or later 8 8 Requires PHP: 5.6 … … 14 14 When there is only one search result available for a query, automatically redirect the user to that result. 15 15 16 ## Details 16 This will work with any theme and any posts, pages, or custom post types that would be shown in search results. 17 17 18 This will work with any theme and any objects that would be shown in search results. 18 == Available Filters == 19 19 20 20 You can filter `one_search_result_pre_check` to false if you need to short-circuit and disable the redirect dynamically. 21 21 22 22 23 23 == Installation==24 == == 24 25 25 - Install the plugin. 26 - Better search experience! 26 = 1.1.0 = 27 28 * Added another filter, cleaned up code. 29 30 = 1.0.3 = 31 32 * Initial release -
one-search-result/trunk/one-search-result.php
r2539075 r2586104 3 3 * Plugin Name: One Search Result 4 4 * Description: Automatically send a user to the page or post if it's the only search result available. 5 * Version: 1. 0.35 * Version: 1. 6 6 * Author: Brad Parbs 7 7 * Author URI: https://bradparbs.com/ … … 13 13 */ 14 14 15 if ( ! defined( 'ABSPATH' ) ) { 16 exit; 17 } 15 add_action( 'template_redirect', __NAMESPACE__ . '\\one_search_result' ); 18 16 19 17 /** 20 18 * If there is one search result, redirect to it automatically. 21 19 */ 22 add_action( 23 'template_redirect', 24 function () { 25 // Only apply to the search results. 26 if ( ! is_search() ) { 27 return; 28 } 20 function one_search_result() { 21 // Only apply to the search results. 22 if ( ! is_search() ) { 23 return; 24 } 29 25 30 31 32 33 26 // Filter 'one_search_result_pre_check' if you need to short-circuit it in some cases. 27 if ( ! apply_filters( 'one_search_result_pre_check', true ) ) { 28 return; 29 } 34 30 35 31 global $wp_query; 36 32 37 38 39 40 33 // Make sure we have results before doing anything. 34 if ( ! isset( $wp_query->posts ) || ! isset( $wp_query->post_count ) ) { 35 return; 36 } 41 37 42 43 44 45 38 // If we only have one result, perfect! 39 if ( ! ( 1 === $wp_query->post_count && 1 === count( $wp_query->posts ) ) ) { 40 return; 41 } 46 42 47 48 49 50 51 43 // Grab the permalink and make sure we got it. 44 $dest = get_permalink( $wp_query->posts[0] ); 45 if ( ! $dest ) { 46 return; 47 } 52 48 53 54 wp_safe_redirect( $dest, 302);55 49 // Send it off! 50 ); 51 exit; 56 52 57 } 58 ); 53 } -
one-search-result/trunk/readme.txt
r2539075 r2586104 1 1 === One Search Result === 2 Contributors: bradparbs 2 Contributors: bradparbs 3 3 Tags: search, content, results, redirects 4 4 Requires at least: 5.2 5 Tested up to: 5. 7.26 Stable tag: 1. 0.35 Tested up to: 5. 6 Stable tag: 1. 7 7 License: GPLv2 or later 8 8 Requires PHP: 5.6 … … 14 14 When there is only one search result available for a query, automatically redirect the user to that result. 15 15 16 ## Details 16 This will work with any theme and any posts, pages, or custom post types that would be shown in search results. 17 17 18 This will work with any theme and any objects that would be shown in search results. 18 == Available Filters == 19 19 20 20 You can filter `one_search_result_pre_check` to false if you need to short-circuit and disable the redirect dynamically. 21 21 22 22 23 23 == Installation==24 == == 24 25 25 - Install the plugin. 26 - Better search experience! 26 = 1.1.0 = 27 28 * Added another filter, cleaned up code. 29 30 = 1.0.3 = 31 32 * Initial release
Note: See TracChangeset
for help on using the changeset viewer.