Skip to content

Instantly share code, notes, and snippets.

@rmccue
Forked from anthonycole/gist:591105
Created September 22, 2010 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmccue/591135 to your computer and use it in GitHub Desktop.
Save rmccue/591135 to your computer and use it in GitHub Desktop.
<?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: anthony@radiopicture.com.au )
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' => 'A taxonomy dropdown navigation to the frontend of your website' );
$control_ops = array( 'width' => 200, 'height' => 250 );
parent::WP_Widget( 'taxonomy_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">
<option value="" selected="selected"></option>
<?php
foreach( $terms as $term ) :
?>
<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;
$instance['title'] = strip_tags( stripslashes($new_instance['title']) );
$instance['taxonomy'] = strip_tags( stripslashes($new_instance['taxonomy']) );
return $instance;
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? $instance['title'] : '';
$the_taxonomy = isset( $instance['taxonomy'] ) ? $instance['taxonomy'] : '';
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
</p>
<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,
'public' => true,
);
$taxonomies = get_taxonomies($args, 'object');
foreach($taxonomies as $taxonomy) {
?>
<option <?php selected($the_taxonomy, $taxonomy->name); ?> value="<?php echo $taxonomy->name; ?>"><?php echo $taxonomy->labels->name; ?></option>
<?php
}
?>
</select>
</p>
<?php
}
function do_javascript() {
?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$(".taxonomy-widget-select").change(function(){
var newurl = $("option:selected", this).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