Created
August 13, 2011 05:34
-
-
Save mertonium/1143511 to your computer and use it in GitHub Desktop.
Copy of the default WordPress category widget - but you can pass a list of IDs to exclude. (Children categories will be excluded as well)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Category Excluder | |
Plugin URI: https://gist.github.com/1143511 | |
Description: Added a way to pass a list of IDs to be excluded from the default Wordpress Categories widget. | |
Author: @mertonium | |
Version: 0.1 | |
Author URI: http://mertonium.com | |
*/ | |
/** | |
* [Custom version of] Categories widget class | |
* | |
* @since 2.8.0 | |
*/ | |
class WP_Widget_Category_Excluder extends WP_Widget { | |
function WP_Widget_Category_Excluder() { | |
$widget_ops = array( 'classname' => 'widget_categoryexcluder', 'description' => __( "A list or dropdown of categories" ) ); | |
$this->WP_Widget('categoryexcluder', __('Category Excluder'), $widget_ops); | |
} | |
function widget( $args, $instance ) { | |
extract( $args ); | |
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Category Excluder' ) : $instance['title'], $instance, $this->id_base); | |
$c = $instance['count'] ? '1' : '0'; | |
$h = $instance['hierarchical'] ? '1' : '0'; | |
$d = $instance['dropdown'] ? '1' : '0'; | |
$exclude_list = $instance['exclude_list']; | |
echo $before_widget; | |
if ( $title ) | |
echo $before_title . $title . $after_title; | |
$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'exclude_tree' => $exclude_list); | |
if ( $d ) { | |
$cat_args['show_option_none'] = __('Select Category'); | |
wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args)); | |
?> | |
<script type='text/javascript'> | |
/* <![CDATA[ */ | |
var dropdown = document.getElementById("cat"); | |
function onCatChange() { | |
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) { | |
location.href = "<?php echo home_url(); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value; | |
} | |
} | |
dropdown.onchange = onCatChange; | |
/* ]]> */ | |
</script> | |
<?php | |
} else { | |
?> | |
<ul> | |
<?php | |
$cat_args['title_li'] = ''; | |
wp_list_categories(apply_filters('widget_categories_args', $cat_args)); | |
?> | |
</ul> | |
<?php | |
} | |
echo $after_widget; | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags($new_instance['title']); | |
$instance['count'] = !empty($new_instance['count']) ? 1 : 0; | |
$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0; | |
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0; | |
$instance['exclude_list'] = strip_tags($new_instance['exclude_list']); | |
return $instance; | |
} | |
function form( $instance ) { | |
//Defaults | |
$instance = wp_parse_args( (array) $instance, array( 'title' => '') ); | |
$title = esc_attr( $instance['title'] ); | |
$count = isset($instance['count']) ? (bool) $instance['count'] :false; | |
$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false; | |
$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false; | |
$exclude_list = esc_attr( $instance['exclude_list'] ); | |
?> | |
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> | |
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> /> | |
<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br /> | |
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> /> | |
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br /> | |
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> /> | |
<label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p> | |
<input type="text" class="widefat" id="<?php echo $this->get_field_id('exclude_list'); ?>" name="<?php echo $this->get_field_name('exclude_list'); ?>" value="<?php echo $exclude_list; ?>" /> | |
<label for="<?php echo $this->get_field_id('exclude_list'); ?>"><?php _e( 'Exclude IDs (comma separated)' ); ?></label></p> | |
<?php | |
} | |
} | |
add_action('widgets_init', create_function('', 'return register_widget("WP_Widget_Category_Excluder");')); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup
Note
If an excluded category has sub categories, they will be excluded too.