Create a dropdown of custom taxonomies in WordPress (the easy way)

So you’ve been busy taking advantage of custom post type functionalities in WordPress since mid 2010. And of course you’re using custom taxonomies too, right? Of course you are.

If you’re a theme or plugin developer you may have ran across the need to populate a dropdown list of your custom taxonomies. Essentially there are two different (easy) ways to accomplish this. One you always hear about and the other you don’t.

Method #1

Since WP 2.1 the wp_dropdown_categories function has been around but in WP 3.0 the taxonomy argument was introduced. So just calling this function and using the taxonomy argument is probably the absolute easiest way to populate a dropdown list of your custom taxonomies.

<?php wp_dropdown_categories('taxonomy=my_custom_taxonomy'); ?>

This method is great if you need the output of your dropdown values to be the category ID. Because this is the HTML that will be generated:

<select name="cat" class="postform">
	<option value="3">Tax A</option>
	<option value="14">Tax B</option>
	<option value="26">Tax C</option>
	<option value="29">Tax D</option>
</select>

However, let’s say you want your option value output to be the taxonomy’s slug instead of the ID. Well, that’s impossible to achieve using the wp_dropdown_categories function.

Peering into the WordPress core we see that this function is using a walker class called Walker_CategoryDropdown. This walker is designed to output only the ID as the value for each dropdown item. There is not an argument in the function to control value output.

Method #2

That’s where Method #2 comes in. We’ll have to write our own custom function that will generate the dropdown so we can output each option value as a slug:

<?php

function custom_taxonomy_dropdown($taxonomy) {
	$terms = get_terms($taxonomy);
	echo '<select name="'.$taxonomy.'" class="postform">';
	foreach ($terms as $term) {
		echo '<option value="'.$term->slug.'">'.$term->name.'</option>';
	}
	echo '</select>';
}

?>

So, now that we’ve got a cool custom function, we can call anywhere in our code like so:

<?php custom_taxonomy_dropdown('my_custom_taxonomy'); ?>

Expansions on Method #2

If you’re a coding rockstar you can take Method #2 even further by making room for more parameters. This will give you even more control and make it function more like wp_dropdown_categories does:

<?php

function custom_taxonomy_dropdown($taxonomy, $orderby, $order, $limit, $name, $show_option_all, $show_option_none) {
	$args = array(
		'orderby' => $orderby,
		'order' => $order,
		'number' => $limit
	);
	$terms = get_terms($taxonomy, $args);
	echo '<select name="'.$name.'" class="postform">';
	if($show_option_all) echo '<option value="0">'.$show_option_all.'</option>';
	if($show_option_none) echo '<option value="-1">'.$show_option_none.'</option>';
	foreach ($terms as $term) {
		echo '<option value="'.$term->slug.'">'.$term->name.'</option>';
	}
	echo '</select>';
}

?>

Then call it in your code like so:

<?php custom_taxonomy_dropdown('my_custom_taxonomy', 'date', 'DESC', '5', 'my_custom_taxonomy', 'Select All', 'Select None'); ?>

As you can see, WordPress has done a fabulous job of making room for you and I to do pretty much whatever we want.

To reference all available arguments and parameters, please see:
http://codex.wordpress.org/Function_Reference/wp_dropdown_categories
http://codex.wordpress.org/Function_Reference/get_terms

Was this code helpful to you? Let me know in the comments!

How to remove an auto-complete URL from Chrome on a Mac

If you’re at all like me, you get very annoyed at little things :)

Let’s imagine you love to browse with Chrome on your beloved Mac and you frequently visit Facebook. So you type the letters “fa” in your browser’s address bar and *bam!* the URL auto-completes to read “facebook.com”. It’s a wonderful thing. But sometimes it’s not so wonderful.

Some users are reporting that when you first startup Safari or Chrome on a Mac the browser may take a while to load those auto-complete gems from your history. That’s not good when you’re used to typing “fa” and rapidly pressing “Enter”.

Oops! Now you’ve just asked your browser to visit “fa” (which does not exist) and from this point on “facebook.com” will show up as the second auto-complete option. Yikes! How do we remove that darn “fa” entry now showing above the actual entry we want?

There is hope.

On a PC everyone seems to have the answer. You simply highlight the entry and press “Shift+Delete”. But us Mac users have an extra step that no one seems to be talking about.

On a Mac, you need to highlight the entry and then press “Fn + Shift + Del”. Now, you might need to wait a bit, or restart the browser to see it removed but I found it to work perfectly.

Did this work for you? Let me know.

Create a shortcode for displaying Order History on your Cart66 site

I’m a fan of Cart66. It’s easy to integrate and has a fair amount of features that make it a flexible e-commerce and member management solution for WordPress.

However, there’s one pretty big feature that Cart66 is missing: the ability to display the Order History of a logged in user.

What? Really?

Yes. Really.

This doesn’t really require a lot of explanation. It was easy to achieve with a simple SQL query and a custom loop. And all without editing the Cart66 core! So you can keep updating the plugin without worry. :)

Websites that sell digital goods will especially find this handy since users will be able to view their past receipts to re-download purchases.

Just insert this code into the functions.php file of your WordPress theme and use the shortcode [order_history] on a member’s only page to show the Order History for that user. Enjoy!

The original code for this idea came from a post by Alison Barrett.

// Cart66 order history shortcode
function cart66_order_history( $atts, $content = null ) {
	extract( shortcode_atts( array(), $atts ) );
	global $wpdb;
	$results = $wpdb->get_results( "SELECT ouid, ordered_on, trans_id, total, status FROM " . $wpdb->prefix . "cart66_orders WHERE account_id = " . Cart66Session::get( 'Cart66AccountId' ) . ' ORDER BY ordered_on DESC' );
	foreach($results as $order){
		$data .= '<tr><td>' . $order->trans_id . '</td><td>' . date( 'F j, Y', strtotime( $order->ordered_on ) ) . '</td><td>' . $order->total . '</td><td>' . ucwords( $order->status ) . '</td><td><a href="' . get_bloginfo( 'url' ) . '/store/receipt/?ouid=' . $order->ouid . '" title="Click to view receipt" target="_blank">View Receipt</a></td></tr>';
	}
	$table = '<table id="viewCartTable">
				<thead>
					<tr>
						<th>Order Number</th>
						<th>Date</th>
						<th>Total</th>
						<th>Order Status</th>
						<th>Receipt</th>
					</tr>
				</thead>
				<tbody>' . $data . '</tbody></table>';
	return $table;
}
add_shortcode('order_history', 'cart66_order_history');

Remove specific admin menu items in WordPress

Of course I love running the daily grind over at ChurchThemes, but every once in a (great) while I do take on a side project for a friend. And each time, I’m reminded how truly different doing client work is from being a theme publisher.

Sometimes it’s best – especially when you’re using WordPress as a CMS – to remove those unwanted admin menus that create clutter for clients. They are never going to use them so why confuse their admin experience? For example: if the client isn’t going to blog, why include the Posts and Comments menus?

Just insert this code into the functions.php file of your WordPress theme and *bam!* no more clutter.

(Make sure to remove the items in the $hidden array you don’t want to hide, this is just an example so you can see what’s possible) Enjoy!

// Remove Specific Admin Menu Items
function remove_menu_items() {
	global $menu;
	$hidden = array(
		__('Posts'),
		__('Media'),
		__('Links'),
		__('Pages'),
		__('Comments'),
		__('Plugins'),
		__('Users'),
		__('Tools')
	);
	end ($menu);
	while (prev($menu)) {
		$value = explode(' ', $menu[key($menu)][0]);
		if(in_array($value[0] != NULL?$value[0]:"", $hidden)){
			unset($menu[key($menu)]);
		}
	}
}
add_action('admin_menu', 'remove_menu_items');