Created
September 22, 2010 03:49
-
-
Save anthonycole/591105 to your computer and use it in GitHub Desktop.
This file contains 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: Taxononmy Navigation Dropdown | |
Version: 0.1 | |
Plugin URI: http://anthonycole.me/wordpress/taxonomy-nav-dropdown/ | |
Description: This plugin allows you to add a simple widget linking all of your taxonomies together. | |
Author: Anthony Cole | |
Author URI: http://anthonycole.me/ | |
Copyright 2010 Anthony Cole ( email: [email protected] ) | |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
*/ | |
class Taxonomy_Widget extends WP_Widget { | |
function Taxonomy_Widget() { | |
$widget_ops = array( 'classname' => 'tnd-widget', 'description' => 'Allows you to add a taxonomy dropdown navigation to the frontend of your website' ); | |
$control_ops = array( 'width' => 200, 'height' => 250, 'id_base' => '' ); | |
$this->WP_Widget( 'Taxononmy_Nav', 'Taxonomy Nav', $widget_ops, $control_ops ); | |
add_action('wp_head', array($this, 'do_javascript') ); | |
} | |
function widget( $args, $instance ) { | |
extract( $args ); | |
echo $before_widget; | |
$title = apply_filters('widget_title', $instance['title'] ); | |
if ( $title ) { | |
echo $before_title . $title . $after_title; | |
} | |
$terms = get_terms(array($instance['taxonomy'])); | |
?> | |
<select name="<?php echo $instance['taxonomy']; ?>" class="taxonomy-widget-select"> | |
<?php | |
foreach( $terms as $terms ) : | |
?> | |
<option value="<?php echo get_term_link( $term->slug, $instance['taxonomy'] ); ?>"><?php echo $term->name; ?></option> | |
<?php | |
endforeach; | |
?> | |
</select> | |
<?php | |
echo $after_widget; | |
} | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
foreach ( array('taxonomy') as $val ) { | |
$instance[$val] = strip_tags( $new_instance[$val] ); | |
} | |
return $instance; | |
} | |
function form( $instance ) { | |
$defaults = array( | |
'taxonomy' => '', | |
); | |
$taxonomies = array( | |
'_builtin' => false, | |
'public' => true, | |
); | |
$instance = wp_parse_args( (array) $instance, $defaults ); ?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e("Taxonomy"); ?>:</label> | |
<select id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>"> | |
<?php | |
$args = array( | |
'_builtin' => false, | |
); | |
$taxonomies = get_taxonomies($args, 'object'); | |
foreach($taxonomies as $taxonomy) { | |
?> | |
<option <?php selected($instance['taxonomy'], $taxonomy->name); ?> value="<?php echo $taxonomy->slug; ?>"><?php echo $taxonomy->labels->name; ?></option> | |
<?php | |
} | |
?> | |
</select> | |
</p> | |
<?php | |
} | |
function do_javascript() { | |
?> | |
<script type="text/javascript"> | |
jQuery(".taxonomy-widget-select").change(function(){ | |
var newurl = $("parent option:selected").attr("value"); | |
window.location.href = newurl; | |
}); | |
</script> | |
<?php | |
} | |
} | |
function tnd_widget_func() { | |
register_widget( 'Taxonomy_Widget' ); | |
} | |
add_action( 'widgets_init', 'tnd_widget_func' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment