1 | <?php |
---|
2 | /** |
---|
3 | * Plugin Name: Disable Search |
---|
4 | * Version: 2.1.1 |
---|
5 | * Plugin URI: https://coffee2code.com/wp-plugins/disable-search/ |
---|
6 | * Author: Scott Reilly |
---|
7 | * Author URI: https://coffee2code.com/ |
---|
8 | * Text Domain: disable-search |
---|
9 | * License: GPLv2 or later |
---|
10 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
---|
11 | * Description: Disable the built-in front-end search capabilities of WordPress. |
---|
12 | * |
---|
13 | * Compatible with WordPress 4.6 through 6.8+, and PHP through at least 8.3+. |
---|
14 | * |
---|
15 | * =>> Read the accompanying readme.txt file for instructions and documentation. |
---|
16 | * =>> Also, visit the plugin's homepage for additional information and updates. |
---|
17 | * =>> Or visit: https://wordpress.org/plugins/disable-search/ |
---|
18 | * |
---|
19 | * @package Disable_Search |
---|
20 | * @author Scott Reilly |
---|
21 | * @version 2.1.1 |
---|
22 | */ |
---|
23 | |
---|
24 | /* |
---|
25 | Copyright (c) 2008-2025 by Scott Reilly (aka coffee2code) |
---|
26 | |
---|
27 | This program is free software; you can redistribute it and/or |
---|
28 | modify it under the terms of the GNU General Public License |
---|
29 | as published by the Free Software Foundation; either version 2 |
---|
30 | of the License, or (at your option) any later version. |
---|
31 | |
---|
32 | This program is distributed in the hope that it will be useful, |
---|
33 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
34 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
35 | GNU General Public License for more details. |
---|
36 | |
---|
37 | You should have received a copy of the GNU General Public License |
---|
38 | along with this program; if not, write to the Free Software |
---|
39 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
40 | */ |
---|
41 | |
---|
42 | defined( 'ABSPATH' ) or die(); |
---|
43 | |
---|
44 | if ( ! class_exists( 'c2c_DisableSearch' ) ) : |
---|
45 | |
---|
46 | class c2c_DisableSearch { |
---|
47 | |
---|
48 | /** |
---|
49 | * Returns version of the plugin. |
---|
50 | * |
---|
51 | * @since 1.3 |
---|
52 | */ |
---|
53 | public static function version() { |
---|
54 | return '2.1.1'; |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Prevent instantiation. |
---|
59 | * |
---|
60 | * @since 1.6 |
---|
61 | */ |
---|
62 | private function __construct() {} |
---|
63 | |
---|
64 | /** |
---|
65 | * Prevent unserializing an instance of the class. |
---|
66 | * |
---|
67 | * @since 1.6 |
---|
68 | * @since 1.8.3 Changed method visibility from private to public and throw exception if invoked. |
---|
69 | */ |
---|
70 | public function __wakeup() { |
---|
71 | /* translators: %s: Name of plugin class. */ |
---|
72 | throw new Error( esc_html( sprintf( __( '%s cannot be unserialized.', 'disable-search' ), __CLASS__ ) ) ); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Initializes the plugin. |
---|
77 | */ |
---|
78 | public static function init() { |
---|
79 | // Register hooks. |
---|
80 | add_action( 'widgets_init', array( __CLASS__, 'disable_search_widget' ), 1 ); |
---|
81 | if ( ! is_admin() ) { |
---|
82 | add_action( 'parse_query', array( __CLASS__, 'parse_query' ), 5 ); |
---|
83 | } |
---|
84 | add_filter( 'get_search_form', '__return_empty_string', 999 ); |
---|
85 | |
---|
86 | add_action( 'admin_bar_menu', array( __CLASS__, 'admin_bar_menu' ), 99999 ); |
---|
87 | |
---|
88 | add_filter( 'disable_wpseo_json_ld_search', '__return_true' ); |
---|
89 | |
---|
90 | // Disable core search block. |
---|
91 | add_action( 'init', array( __CLASS__, 'disable_core_search_block' ), 11 ); |
---|
92 | add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_assets' ) ); |
---|
93 | |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Disables the built-in WP search widget. |
---|
98 | */ |
---|
99 | public static function disable_search_widget() { |
---|
100 | unregister_widget( 'WP_Widget_Search' ); |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Unregisters the core/search block (at least for PHP). |
---|
105 | * |
---|
106 | * Though this technically works (the block gets unregistered), it doesn't |
---|
107 | * actually disable the block, which is at least still available via JS and |
---|
108 | * thus is functionally equivalent to this doing nothing. |
---|
109 | * |
---|
110 | * The use of the `'allowed_block_types_all'` filter seems ideal for this |
---|
111 | * sort of thing, but it has its issues at present (see associated link). |
---|
112 | * |
---|
113 | * @link https://.com/WordPress/gutenberg/issues/12931 |
---|
114 | * @since 2.0 |
---|
115 | */ |
---|
116 | public static function disable_core_search_block() { |
---|
117 | if ( function_exists( 'unregister_block_type' ) ) { |
---|
118 | $block = 'core/search'; |
---|
119 | if ( WP_Block_Type_Registry::get_instance()->is_registered( $block ) ) { |
---|
120 | unregister_block_type( $block ); |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Enqueues block editor assets, notable to disable the search block. |
---|
127 | * |
---|
128 | * @since 2.0 |
---|
129 | */ |
---|
130 | public static function enqueue_block_editor_assets() { |
---|
131 | wp_enqueue_script( 'disable-search-js', plugins_url( '/assets/js/disable-search.js', __FILE__ ), array( 'wp-blocks', 'wp-dom' ), self::version(), true ); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Unsets all search-related variables in WP_Query object and sets the |
---|
136 | * request as a 404 if a search was attempted. |
---|
137 | * |
---|
138 | * @param WP_Query $obj A query object. |
---|
139 | */ |
---|
140 | public static function parse_query( $obj ) { |
---|
141 | if ( $obj->is_search && $obj->is_main_query() ) { |
---|
142 | unset( $_GET['s'] ); |
---|
143 | unset( $_POST['s'] ); |
---|
144 | unset( $_REQUEST['s'] ); |
---|
145 | unset( $obj->query['s'] ); |
---|
146 | $obj->set( 's', '' ); |
---|
147 | $obj->is_search = false; |
---|
148 | $obj->set_404(); |
---|
149 | status_header( 404 ); |
---|
150 | nocache_headers(); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Removes the search item from the admin bar. |
---|
156 | * |
---|
157 | * @since 1.6 |
---|
158 | * |
---|
159 | * @param WP_Admin_Bar $wp_admin_bar The WP admin bar object. |
---|
160 | */ |
---|
161 | public static function admin_bar_menu( $wp_admin_bar ) { |
---|
162 | $wp_admin_bar->remove_menu( 'search' ); |
---|
163 | } |
---|
164 | |
---|
165 | } // end c2c_DisableSearch |
---|
166 | |
---|
167 | add_action( 'plugins_loaded', array( 'c2c_DisableSearch', 'init' ) ); |
---|
168 | |
---|
169 | endif; // end if !class_exists() |
---|