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