-
Notifications
You must be signed in to change notification settings - Fork 1
/
category-excluder.php
103 lines (92 loc) · 3.97 KB
/
category-excluder.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
<?php
// add_action('admin_menu', 'ce_admin_menu');
add_filter('pre_get_posts','ce_exclude_categories');
function ce_process() {
//echo '<pre>'; print_r( $_POST );
if( !$_POST[ 'exclude_main' ] ) {
$_POST[ 'exclude_main' ] = array();
}
if( !$_POST[ 'exclude_feed' ] ) {
$_POST[ 'exclude_feed' ] = array();
}
if( !$_POST[ 'exclude_archives' ] ) {
$_POST[ 'exclude_archives' ] = array();
}
$options['exclude_main'] = $_POST[ 'exclude_main' ];
$options['exclude_feed'] = $_POST[ 'exclude_feed' ];
$options['exclude_archives'] = $_POST[ 'exclude_archives' ];
update_option('ceExcludes', $options);
$message = "<div class='updated'><p>Excludes successfully updated</p></div>";
return $message;
}
function ce_get_options(){
$defaults = array();
$defaults['exclude_main'] = array();
$defaults['exclude_feed'] = array();
$defaults['exclude_archives'] = array();
$options = get_option('ceExcludes');
if (!is_array($options)){
$options = $defaults;
update_option('ceExcludes', $options);
}
return $options;
}
function ce_exclude_categories($query) {
$options = ce_get_options();
if ($query->is_home) {
$query->set('cat', implode( ', ', $options[ 'exclude_main' ] ) );
}
if ($query->is_feed) {
$query->set('cat', implode(', ', $options[ 'exclude_feed' ] ) );
}
if ($query->is_archive) {
$query->set('cat', implode(', ', $options[ 'exclude_archives' ] ) );
}
return $query;
}
function ce_controlpanel() {
if( $_POST[ 'ce' ] ) {
$message = ce_process();
}
$options = ce_get_options();
?>
<div class="wrap">
<h2>Category Excluder Options</h2>
<?php echo $message ?>
<p>Use this section allows you to select the categories you wish to exclude and where you would like to exclude them from.</p>
<p>Note: If a post is in more the one category, it will be excluded if it matches any of the excluded categories</p>
<form action="themes.php?page=control-panel.php" method="post">
<table class="widefat">
<thead>
<tr>
<th scope="col">Category</th>
<th scope="col">Exclude from Main Page?</th>
<th scope="col">Exclude from Feeds?</th>
<th scope="col">Exclude from Archives?</th>
</tr>
</thead>
<tbody id="the-list">
<?php
//print_r( get_categories() );
$cats = get_categories();
$alt = 0;
foreach( $cats as $cat ) {
?>
<tr<?php if ( $alt == 1 ) { echo ' class="alternate"'; $alt = 0; } else { $alt = 1; } ?>>
<th scope="row"><?php echo $cat->cat_name; //. ' (' . $cat->cat_ID . ')'; ?></th>
<td>
<input type="checkbox" name="exclude_main[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( '-' . $cat->cat_ID, $options['exclude_main'] ) ) { echo 'checked="true" '; } ?>/>
</td>
<td><input type="checkbox" name="exclude_feed[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( '-' . $cat->cat_ID, $options['exclude_feed'] ) ) { echo 'checked="true" '; } ?>/></td>
<td><input type="checkbox" name="exclude_archives[]" value="-<?php echo $cat->cat_ID ?>" <?php if ( in_array( '-' . $cat->cat_ID, $options['exclude_archives'] ) ) { echo 'checked="true" '; } ?>/></td>
</tr>
<?php
}
?>
</table>
<p class="submit"><input type="submit" value="Update" /></p>
<input type="hidden" name="ce" value="true" />
</form>
</div><?php
}
?>