-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoindex-manager.php
514 lines (470 loc) · 19.3 KB
/
noindex-manager.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/*
Plugin Name: Noindex Manager
Plugin URI: https://www.littlebizzy.com/plugins/noindex-manager
Description: Noindex thin WordPress content
Version: 1.5.0
Author: LittleBizzy
Author URI: https://www.littlebizzy.com
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
GitHub Plugin URI: littlebizzy/noindex-manager
Primary Branch: main
*/
// prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// disable wordpress.org updates for this plugin
add_filter( 'gu_override_dot_org', function( $overrides ) {
$overrides[] = 'noindex-manager/noindex-manager.php';
return $overrides;
}, 999 );
// Create settings page in WP Admin
function noindex_manager_add_admin_menu() {
add_options_page(
'Noindex Manager Settings',
'Noindex Manager',
'manage_options',
'noindex-manager',
'noindex_manager_options_page'
);
}
add_action( 'admin_menu', 'noindex_manager_add_admin_menu' );
// Register settings
function noindex_manager_settings_init() {
register_setting( 'noindex_manager', 'noindex_manager_settings', [
'sanitize_callback' => 'noindex_manager_sanitize_settings'
]);
// WordPress options section
add_settings_section(
'noindex_manager_wp_section',
null,
null,
'noindex_manager_wp'
);
// Noindex WordPress Tag Archives
add_settings_field(
'noindex_tag',
__( 'WordPress Tag Archives', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_wp',
'noindex_manager_wp_section',
[
'label_for' => 'noindex_tag',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex WordPress Date Archives
add_settings_field(
'noindex_date',
__( 'WordPress Date Archives', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_wp',
'noindex_manager_wp_section',
[
'label_for' => 'noindex_date',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex WordPress Attachment Pages
add_settings_field(
'noindex_attachments',
__( 'WordPress Attachment Pages', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_wp',
'noindex_manager_wp_section',
[
'label_for' => 'noindex_attachments',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex WordPress Author Pages
add_settings_field(
'noindex_author',
__( 'WordPress Author Pages', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_wp',
'noindex_manager_wp_section',
[
'label_for' => 'noindex_author',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// WooCommerce options section
add_settings_section(
'noindex_manager_woocommerce_section',
null,
null,
'noindex_manager_woocommerce'
);
// Noindex WooCommerce Grouped Products
add_settings_field(
'noindex_grouped_products',
__( 'WooCommerce Grouped Products', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_grouped_products',
'default' => 'index',
'recommended' => 'index'
]
);
// Noindex WooCommerce Product Attribute Archives (Default to Index)
add_settings_field(
'noindex_woocommerce',
__( 'WooCommerce Product Attribute Archives', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_woocommerce',
'default' => 'index', // Updated to Index
'recommended' => 'index'
]
);
// Noindex WooCommerce Product Categories (Default to Index)
add_settings_field(
'noindex_product_categories',
__( 'WooCommerce Product Categories', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_product_categories',
'default' => 'index', // Updated to Index
'recommended' => 'index'
]
);
// Noindex WooCommerce Product Tags
add_settings_field(
'noindex_product_tags',
__( 'WooCommerce Product Tags', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_product_tags',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex WooCommerce Shipping Classes
add_settings_field(
'noindex_shipping_classes',
__( 'WooCommerce Shipping Classes', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_shipping_classes',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex WooCommerce Checkout Page
add_settings_field(
'noindex_checkout',
__( 'WooCommerce Checkout Page', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_checkout',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex WooCommerce Cart Page
add_settings_field(
'noindex_cart',
__( 'WooCommerce Cart Page', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_cart',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex WooCommerce My Account Page
add_settings_field(
'noindex_my_account',
__( 'WooCommerce My Account Page', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_my_account',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex WooCommerce Order Confirmation Page
add_settings_field(
'noindex_order_confirmation',
__( 'WooCommerce Order Confirmation Page', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_order_confirmation',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex WooCommerce Out-of-Stock Products (Default to Index)
add_settings_field(
'noindex_out_of_stock',
__( 'WooCommerce Out-of-Stock Products', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_woocommerce',
'noindex_manager_woocommerce_section',
[
'label_for' => 'noindex_out_of_stock',
'default' => 'index', // Updated to Index
'recommended' => 'index'
]
);
// bbPress options section
add_settings_section(
'noindex_manager_bbpress_section',
null,
null,
'noindex_manager_bbpress'
);
// Noindex bbPress Single Forums
add_settings_field(
'noindex_bbpress_forums',
__( 'bbPress Single Forums', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_bbpress',
'noindex_manager_bbpress_section',
[
'label_for' => 'noindex_bbpress_forums',
'default' => 'index',
'recommended' => 'index'
]
);
// Noindex bbPress Topic Archive
add_settings_field(
'noindex_bbpress_topic_archive',
__( 'bbPress Topic Archive', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_bbpress',
'noindex_manager_bbpress_section',
[
'label_for' => 'noindex_bbpress_topic_archive',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex bbPress Topic Tag Archives
add_settings_field(
'noindex_bbpress_topic_tag_archive',
__( 'bbPress Topic Tag Archives', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_bbpress',
'noindex_manager_bbpress_section',
[
'label_for' => 'noindex_bbpress_topic_tag_archive',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex bbPress User Profiles
add_settings_field(
'noindex_bbpress_user',
__( 'bbPress User Profiles', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_bbpress',
'noindex_manager_bbpress_section',
[
'label_for' => 'noindex_bbpress_user',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex bbPress Topics with 2 or fewer replies
add_settings_field(
'noindex_bbpress_topics',
__( 'bbPress Topics with 2 or Fewer Replies', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_bbpress',
'noindex_manager_bbpress_section',
[
'label_for' => 'noindex_bbpress_topics',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex bbPress Views
add_settings_field(
'noindex_bbpress_views',
__( 'bbPress Views', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_bbpress',
'noindex_manager_bbpress_section',
[
'label_for' => 'noindex_bbpress_views',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
// Noindex bbPress Replies
add_settings_field(
'noindex_bbpress_replies',
__( 'bbPress Replies', 'noindex-manager' ),
'noindex_manager_render_select',
'noindex_manager_bbpress',
'noindex_manager_bbpress_section',
[
'label_for' => 'noindex_bbpress_replies',
'default' => 'noindex',
'recommended' => 'noindex'
]
);
}
add_action( 'admin_init', 'noindex_manager_settings_init' );
// sanitize settings input
function noindex_manager_sanitize_settings( $input ) {
$sanitized_input = [];
// sanitize wordpress options
$sanitized_input['noindex_tag'] = in_array( $input['noindex_tag'], ['noindex', 'index'], true ) ? $input['noindex_tag'] : 'noindex';
$sanitized_input['noindex_date'] = in_array( $input['noindex_date'], ['noindex', 'index'], true ) ? $input['noindex_date'] : 'noindex';
$sanitized_input['noindex_attachments'] = in_array( $input['noindex_attachments'], ['noindex', 'index'], true ) ? $input['noindex_attachments'] : 'noindex';
$sanitized_input['noindex_author'] = in_array( $input['noindex_author'], ['noindex', 'index'], true ) ? $input['noindex_author'] : 'noindex';
// sanitize woocommerce options
$sanitized_input['noindex_grouped_products'] = in_array( $input['noindex_grouped_products'], ['noindex', 'index'], true ) ? $input['noindex_grouped_products'] : 'index';
$sanitized_input['noindex_woocommerce'] = in_array( $input['noindex_woocommerce'], ['noindex', 'index'], true ) ? $input['noindex_woocommerce'] : 'index';
$sanitized_input['noindex_product_categories'] = in_array( $input['noindex_product_categories'], ['noindex', 'index'], true ) ? $input['noindex_product_categories'] : 'index';
$sanitized_input['noindex_product_tags'] = in_array( $input['noindex_product_tags'], ['noindex', 'index'], true ) ? $input['noindex_product_tags'] : 'noindex';
$sanitized_input['noindex_shipping_classes'] = in_array( $input['noindex_shipping_classes'], ['noindex', 'index'], true ) ? $input['noindex_shipping_classes'] : 'noindex';
$sanitized_input['noindex_checkout'] = in_array( $input['noindex_checkout'], ['noindex', 'index'], true ) ? $input['noindex_checkout'] : 'noindex';
$sanitized_input['noindex_cart'] = in_array( $input['noindex_cart'], ['noindex', 'index'], true ) ? $input['noindex_cart'] : 'noindex';
$sanitized_input['noindex_my_account'] = in_array( $input['noindex_my_account'], ['noindex', 'index'], true ) ? $input['noindex_my_account'] : 'noindex';
$sanitized_input['noindex_order_confirmation'] = in_array( $input['noindex_order_confirmation'], ['noindex', 'index'], true ) ? $input['noindex_order_confirmation'] : 'noindex';
$sanitized_input['noindex_out_of_stock'] = in_array( $input['noindex_out_of_stock'], ['noindex', 'index'], true ) ? $input['noindex_out_of_stock'] : 'index';
// sanitize bbpress options
$sanitized_input['noindex_bbpress_forums'] = in_array( $input['noindex_bbpress_forums'], ['noindex', 'index'], true ) ? $input['noindex_bbpress_forums'] : 'index';
$sanitized_input['noindex_bbpress_topic_archive'] = in_array( $input['noindex_bbpress_topic_archive'], ['noindex', 'index'], true ) ? $input['noindex_bbpress_topic_archive'] : 'noindex';
$sanitized_input['noindex_bbpress_topic_tag_archive'] = in_array( $input['noindex_bbpress_topic_tag_archive'], ['noindex', 'index'], true ) ? $input['noindex_bbpress_topic_tag_archive'] : 'noindex';
$sanitized_input['noindex_bbpress_user'] = in_array( $input['noindex_bbpress_user'], ['noindex', 'index'], true ) ? $input['noindex_bbpress_user'] : 'noindex';
$sanitized_input['noindex_bbpress_topics'] = in_array( $input['noindex_bbpress_topics'], ['noindex', 'index'], true ) ? $input['noindex_bbpress_topics'] : 'noindex';
$sanitized_input['noindex_bbpress_views'] = in_array( $input['noindex_bbpress_views'], ['noindex', 'index'], true ) ? $input['noindex_bbpress_views'] : 'noindex';
$sanitized_input['noindex_bbpress_replies'] = in_array( $input['noindex_bbpress_replies'], ['noindex', 'index'], true ) ? $input['noindex_bbpress_replies'] : 'noindex';
return $sanitized_input;
}
// render select dropdown for each option
function noindex_manager_render_select( $args ) {
$options = get_option( 'noindex_manager_settings' );
$current_value = isset( $options[ $args['label_for'] ] ) ? $options[ $args['label_for'] ] : $args['default'];
?>
<select id="<?php echo esc_attr( $args['label_for'] ); ?>" name="noindex_manager_settings[<?php echo esc_attr( $args['label_for'] ); ?>]">
<option value="noindex" <?php selected( $current_value, 'noindex' ); ?>><?php esc_html_e( 'Noindex', 'noindex-manager' ); ?></option>
<option value="index" <?php selected( $current_value, 'index' ); ?>><?php esc_html_e( 'Index', 'noindex-manager' ); ?></option>
</select>
<span class="description" style="margin-left: 10px;">
<?php printf( esc_html__( 'Recommended: %s', 'noindex-manager' ), esc_html( ucfirst( $args['recommended'] ) ) ); ?>
</span>
<?php
}
// Output settings page with tabs
function noindex_manager_options_page() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'Noindex Manager Settings', 'noindex-manager' ); ?></h1>
<h2 class="nav-tab-wrapper">
<a href="#wp-settings" class="nav-tab nav-tab-active"><?php esc_html_e( 'WordPress', 'noindex-manager' ); ?></a>
<a href="#woocommerce-settings" class="nav-tab"><?php esc_html_e( 'WooCommerce', 'noindex-manager' ); ?></a>
<a href="#bbpress-settings" class="nav-tab"><?php esc_html_e( 'bbPress', 'noindex-manager' ); ?></a>
</h2>
<form action="options.php" method="post">
<div id="wp-settings" class="tab-content" style="display:block;">
<?php
settings_fields( 'noindex_manager' );
do_settings_sections( 'noindex_manager_wp' );
?>
</div>
<div id="woocommerce-settings" class="tab-content" style="display:none;">
<?php
settings_fields( 'noindex_manager' );
do_settings_sections( 'noindex_manager_woocommerce' );
?>
</div>
<div id="bbpress-settings" class="tab-content" style="display:none;">
<?php
settings_fields( 'noindex_manager' );
do_settings_sections( 'noindex_manager_bbpress' );
?>
</div>
<?php submit_button(); ?>
</form>
<style>
.nav-tab:focus, .nav-tab:active {
outline: none;
box-shadow: none;
}
</style>
<script>
jQuery(document).ready(function($) {
$('.nav-tab').on('click', function(e) {
e.preventDefault();
$('.nav-tab-active').removeClass('nav-tab-active');
$(this).addClass('nav-tab-active');
$('.tab-content').hide();
$($(this).attr('href')).show();
});
});
</script>
</div>
<?php
}
// main function to noindex grouped products and other settings
function noindex_thin_content() {
$options = get_option( 'noindex_manager_settings' );
// noindex for woocommerce grouped products
$noindex_grouped_products = isset( $options['noindex_grouped_products'] ) ? $options['noindex_grouped_products'] : 'index';
if ( class_exists( 'WooCommerce' ) && is_product() && wc_get_product()->is_type( 'grouped' ) && $noindex_grouped_products === 'noindex' ) {
wp_no_robots();
return;
}
// noindex for wordpress date archives
$noindex_date = isset( $options['noindex_date'] ) ? $options['noindex_date'] : 'noindex';
if ( is_date() && $noindex_date === 'noindex' ) {
wp_no_robots();
return;
}
// noindex for wordpress attachment pages
$noindex_attachments = isset( $options['noindex_attachments'] ) ? $options['noindex_attachments'] : 'noindex';
if ( is_attachment() && $noindex_attachments === 'noindex' ) {
wp_no_robots();
return;
}
// noindex for wordpress author pages
$noindex_author = isset( $options['noindex_author'] ) ? $options['noindex_author'] : 'noindex';
if ( is_author() && $noindex_author === 'noindex' ) {
wp_no_robots();
return;
}
// (additional noindex logic can be added here)
}
// Hardcode noindex for all search result pages (WordPress, WooCommerce, bbPress)
function noindex_search_results() {
if ( is_search() ) {
wp_no_robots();
}
}
add_action( 'wp_head', 'noindex_thin_content' );
add_action( 'wp_head', 'noindex_search_results' );
// Ref: ChatGPT
// https://bbpress.org/forums/topic/add-noindex-to-some-forum-pages/
// https://wordpress.stackexchange.com/questions/333424/check-if-page-is-a-woocommerce-attribute
// https://stackoverflow.com/questions/72013417/check-the-woocommerce-product-attribute-on-archive-page-and-do-something
// https://wordpress.org/support/topic/taxonomy_is_product_attribute/