Repository navigation
Expand file tree
/
Copy pathdisable-comments.php
More file actions
334 lines (293 loc) · 8.06 KB
/
Copy pathdisable-comments.php
File metadata and controls
334 lines (293 loc) · 8.06 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Plugin Name: Disable Comments
* Plugin URI: https://github.com/BeAPI/disable-comments
* Description: Completely disables comments functionality in WordPress. Zero configuration needed.
* Version: 1.0.2
* Requires at least: 5.0
* Requires PHP: 7.4
* Author: BeAPI
* Author URI: https://beapi.fr
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: disable-comments
* Domain Path: /languages
* Update URI: false
*
* Inspired by:
* - Disable Comments plugin: https://wordpress.org/plugins/disable-comments/
* - WPBeginner Tutorial: https://www.wpbeginner.com/wp-tutorials/how-to-completely-disable-comments-in-wordpress/
*
* @package DisableComments
*/
namespace BEAPI\DisableComments;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Initialize WordPress hooks.
*
* @since 1.0.0
*
* @return void
*/
function init() {
// Disable support for comments and trackbacks in post types.
add_action( 'admin_init', __NAMESPACE__ . '\\disable_comments_post_types_support' );
// Close comments on the front-end.
add_filter( 'comments_open', '__return_false', 20, 2 );
add_filter( 'pings_open', '__return_false', 20, 2 );
// Hide existing comments.
add_filter( 'comments_array', '__return_empty_array', 10, 2 );
// Remove comments page in menu.
add_action( 'admin_menu', __NAMESPACE__ . '\\remove_comments_menu' );
// Redirect any user trying to access comments page.
add_action( 'admin_init', __NAMESPACE__ . '\\redirect_comments_page' );
// Remove comments links from admin bar.
add_action( 'template_redirect', __NAMESPACE__ . '\\remove_admin_bar_comments' );
add_action( 'admin_init', __NAMESPACE__ . '\\remove_admin_bar_comments' );
// Remove comments metabox from dashboard.
add_action( 'admin_init', __NAMESPACE__ . '\\remove_dashboard_comments_metabox' );
// Remove comments column from posts list.
add_action( 'admin_init', __NAMESPACE__ . '\\remove_comments_column' );
// Remove comment count from "Right Now" dashboard widget.
add_action( 'wp_dashboard_setup', __NAMESPACE__ . '\\remove_comments_dashboard' );
// Remove Recent Comments widget.
add_action( 'widgets_init', __NAMESPACE__ . '\\remove_recent_comments_widget' );
// Remove comments feed.
add_action( 'template_redirect', __NAMESPACE__ . '\\disable_comments_feed', 9 );
// Remove comment-reply script.
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\remove_comment_reply_script', 100 );
// Remove X-Pingback header.
add_filter( 'wp_headers', __NAMESPACE__ . '\\remove_pingback_header' );
// Disable XML-RPC methods for comments and pingbacks.
add_filter( 'xmlrpc_methods', __NAMESPACE__ . '\\disable_xmlrpc_methods' );
// Disable Comments REST API Endpoint.
add_filter( 'rest_endpoints', __NAMESPACE__ . '\\filter_rest_endpoints' );
// Disable inserting comments via REST API.
add_filter( 'rest_pre_insert_comment', __NAMESPACE__ . '\\__return_null', 10, 2 );
// Disable Gutenberg comments blocks.
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\disable_comments_blocks' );
}
/**
* Disable comments and trackbacks support for all post types.
*
* @since 1.0.0
*
* @return void
*/
function disable_comments_post_types_support() {
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
if ( post_type_supports( $post_type, 'comments' ) ) {
remove_post_type_support( $post_type, 'comments' );
remove_post_type_support( $post_type, 'trackbacks' );
}
}
}
/**
* Remove comments menu from admin.
*
* @since 1.0.0
*
* @return void
*/
function remove_comments_menu() {
remove_menu_page( 'edit-comments.php' );
}
/**
* Redirect users trying to access comments page.
*
* @since 1.0.0
*
* @return void
*/
function redirect_comments_page() {
global $pagenow;
if ( 'edit-comments.php' === $pagenow ) {
wp_safe_redirect( admin_url() );
exit;
}
}
/**
* Remove comments links from admin bar.
*
* @since 1.0.0
*
* @return void
*/
function remove_admin_bar_comments() {
if ( is_admin_bar_showing() ) {
remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
if ( is_multisite() ) {
add_action( 'admin_bar_menu', __NAMESPACE__ . '\\remove_network_comment_links', 500 );
}
}
}
/**
* Remove comment links from the admin bar in a multisite network.
*
* @since 1.0.0
*
* @param \WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
* @return void
*/
function remove_network_comment_links( $wp_admin_bar ) {
if ( is_user_logged_in() ) {
foreach ( (array) $wp_admin_bar->user->blogs as $blog ) {
$wp_admin_bar->remove_menu( 'blog-' . $blog->userblog_id . '-c' );
}
} else {
// We have no way to know whether the plugin is active on other sites, so only remove this one.
$wp_admin_bar->remove_menu( 'blog-' . get_current_blog_id() . '-c' );
}
}
/**
* Remove comments metabox from dashboard.
*
* @since 1.0.0
*
* @return void
*/
function remove_dashboard_comments_metabox() {
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
/**
* Remove comments column from post types.
*
* @since 1.0.0
*
* @return void
*/
function remove_comments_column() {
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
add_filter( "manage_{$post_type}_posts_columns", __NAMESPACE__ . '\\remove_column_callback' );
}
}
/**
* Callback to remove comments column.
*
* @since 1.0.0
*
* @param array $columns The columns array.
* @return array Modified columns array.
*/
function remove_column_callback( $columns ) {
unset( $columns['comments'] );
return $columns;
}
/**
* Remove comments from dashboard "At a Glance" widget.
*
* @since 1.0.0
*
* @return void
*/
function remove_comments_dashboard() {
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
}
/**
* Unregister Recent Comments widget.
*
* @since 1.0.0
*
* @return void
*/
function remove_recent_comments_widget() {
unregister_widget( 'WP_Widget_Recent_Comments' );
// Remove inline style added by Recent Comments widget.
add_filter( 'show_recent_comments_widget_style', '__return_false' );
}
/**
* Disable comments feed.
*
* @since 1.0.0
*
* @return void
*/
function disable_comments_feed() {
if ( is_comment_feed() ) {
wp_die(
esc_html__( 'Comments are disabled.', 'disable-comments' ),
esc_html__( 'Comments Disabled', 'disable-comments' ),
array( 'response' => 403 )
);
}
}
/**
* Remove comment-reply script.
*
* @since 1.0.0
*
* @return void
*/
function remove_comment_reply_script() {
wp_deregister_script( 'comment-reply' );
}
/**
* Remove X-Pingback header.
*
* @since 1.0.0
*
* @param array $headers The headers array.
* @return array Modified headers array.
*/
function remove_pingback_header( $headers ) {
unset( $headers['X-Pingback'] );
return $headers;
}
/**
* Disable XML-RPC methods for comments and pingbacks.
*
* @since 1.0.0
*
* @param array $methods The XMLRPC methods array.
* @return array Modified methods array.
*/
function disable_xmlrpc_methods( $methods ) {
unset( $methods['pingback.ping'] );
unset( $methods['pingback.extensions.getPingbacks'] );
unset( $methods['wp.newComment'] );
return $methods;
}
/**
* Remove the comments endpoint for the REST API.
*
* @since 1.0.0
*
* @param array $endpoints The available REST API endpoints.
* @return array Modified endpoints array.
*/
function filter_rest_endpoints( $endpoints ) {
if ( isset( $endpoints['comments'] ) ) {
unset( $endpoints['comments'] );
}
if ( isset( $endpoints['/wp/v2/comments'] ) ) {
unset( $endpoints['/wp/v2/comments'] );
}
if ( isset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] ) ) {
unset( $endpoints['/wp/v2/comments/(?P<id>[\d]+)'] );
}
return $endpoints;
}
/**
* Disable Gutenberg comments blocks.
* Enqueues JavaScript to unregister all comment-related blocks from the block editor.
*
* @since 1.0.0
*
* @return void
*/
function disable_comments_blocks() {
wp_enqueue_script(
'disable-comments-blocks',
plugin_dir_url( __FILE__ ) . 'assets/disable-comments-blocks.js',
array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
'1.0.0',
true
);
}
// Initialize the plugin.
init();