WordPress Multi-select Custom Taxonomies
Using a multi-select on my form for editing a Quote on my QuoteTracker project sped up my editing time, as in the end I was able to edit Tags for my Posts on the front end. Here’s how I did it.
The end result, front end editing of my Post Tags via Ajax:
As we can see I’m using the Chosen jQuery plugin by Harvest, Rob Spangler from Sizeable Spaces recommended I use on my TaskTrack project, which is still a work in progress.
Aside from integrating all the JavaScript and CSS styling, which I won’t cover at all in detail, I’ll jump right into determining, which items are pre-selected.
Pseudo Code – WordPress Custom Taxonomy Default Selection
- Pass in our taxonomy
- – If it is an array, extract the items and use those as parameters
- – if multi-select is true, we add the keyword AND most importantly those f***king square brackets.
- Get our terms, use get_terms
- Check for is_wp_error
- Get current terms, use get_the_terms
- (now the headache part, at this point we have two list, one of the current terms for our Post and the other the terms in our database.)
- If we have terms
- – Print out a select box
- – foreach terms as term
- – — (compare our current term with the term in our database)
- – — for i in count(our terms)
- – — – — do they match? yes, assign “selected”
- ….
You should get the idea, I won’t write out the rest of the pseudo code, that fucker really had me going. I’m honestly not surprised since I try to avoid double for loop’s like a coke head avoiding a free plane trip to Bogata, Columbia.
If you don’t want to waste your time coding, since of course you do! You can check out Scribu‘s front-end-edit plugin. He’s fucking crazy, he went god knows how far out of his way to use some other wisywig editor (those are just plain hell to work with I would write my experience’s dev’ing plugins for FCkEditor, but I’d rather eat glass after spending the night with Russian whores and cheap vodka).
Granted Scribu’s plugin does rock, if your into plugins I’d use that.
Code – Care of GitHub Gist
/** * Build an option list of Terms based on a given Taxonomy. * * @package helper * @uses zm_base_get_terms to return the terms with error checking * @param string $taxonomy * @param mixed $value, the value to be used in the form field, can be term_id or term_slug */if ( ! function_exists( 'zm_base_build_options' ) ) :function zm_base_build_options( $taxonomy=null, $value=null ) {
if ( is_null ( $value ) ) $value = 'term_id';
if ( is_array( $taxonomy ) ) extract( $taxonomy );
// white list if ( empty( $prepend ) ) $prepend = null; if ( empty( $extra_data ) ) $extra_data = null;
if ( empty( $extra_css ) ) $extra_css = null;
if ( $multiple ) { $multiple = 'multiple="multiple"'; $multiple_extra = '[]'; } else { $multiple = false; }
if ( !isset( $label ) ) $label = $taxonomy; /** All Terms */ $args = array( 'orderby' => 'name', 'hide_empty' => false );
$terms = get_terms( $taxonomy, $args );
if ( is_wp_error( $terms ) ) {// exit( "Opps..." . $terms->get_error_message() . "..dog, cmon, fix it!" ); $terms = false; }
global $post;
if ( $post ) { $current_terms = get_the_terms( $post->ID, $taxonomy ); } ?> <?php if ( $terms ) : ?> <fieldset class="zm-base-<?php echo $taxonomy; ?>-container <?php echo $taxonomy; ?>-container"> <label class="zm-base-title"><?php echo $label; ?></label> <select name="<?php echo $taxonomy; ?><?php echo $multiple_extra;?>" <?php echo $multiple; ?> <?php echo $extra_data; ?> class="<?php echo $extra_class; ?>" id="" <?php echo $multiple; ?>> <?php // First choice ?> <option value="">-- Choose a <?php echo $taxonomy; ?> --</option> <?php foreach( $terms as $term ) : ?> <?php for ( $i=0, $count=count($current_terms); $i <= $count; $i++ ) : ?> <?php // yes fuckig cryptic, i hate for's in foreach's; ?> <?php $current_terms[$term->term_id]->name ? $temp = $current_terms[$term->term_id]->name : $temp = null; ?> <?php endfor; ?> <?php $term->name == $temp ? $selected = 'selected="selected"' : $selected = null; ?> <option value="<?php echo $prepend; ?><?php echo $term->$value; ?>" data-value="<?php echo $term->slug; ?>" class="taxonomy-<?php echo $taxonomy; ?> term-<?php echo $term->slug; ?> <?php echo $taxonomy; ?>-<?php echo $term->term_id; ?>" <?php echo $selected; ?>> <?php echo $term->name; ?> </option> <?php endforeach; ?> </select> </fieldset> <?php endif; ?><?php }endif;
Note our awesome coke-head brought to you by DJ Raza

