Categories
Code

Categories images plugin modified to save image ID

I very quickly (and badly) hacked up Zahlan’s excellent Categories Images plugin to save image IDs rather than the absolute URL to the image file. This means we can use WordPress’ image resizing as well as its other image meta tools.

Download the categories-images plugin (zip)

Update (10 Oct 12): The first upload had a bit of code left in from my own use of the plugin that limited it to a certain taxonomy. I’ve removed it now so the plugin works on all taxonomies as it should.

The key points are:

  • z_edit_taxonomy_field() and z_add_taxonomy_field()  are modified to have a separate button to upload the image rather than clicking the text box. It means the user can manually type the ID if they wish.
  • z_script() grabs the image ID from thickbox instead of the URL. Because thickbox returns an img tag with the image, the only way to get the ID is to chop it out of the class.
  • z_save_taxonomy_image() validates the image ID before saving in case the user types an ID in the text box.
Categories
Code

Add menu li class of item’s name in WordPress

This adds a class of the item’s name to each list item in a nav menu in WordPress. So, if the page item is called ‘About us’, the li gets an extra class of ‘menu-item-about-us’. Handy for styling.

add_filter('nav_menu_css_class' , 'ed_page_title_class' , 10 , 2);
function ed_page_title_class($classes, $item){
  $classes[] = sanitize_title('menu-item-' . $item->title);
  return $classes;
}
Categories
Code

Add body class if post has a featured image in WordPress

This WordPress function adds a body class ‘has-featured-image’ if the current post has a featured image assigned.

add_action('body_class', 'ed_if_featured_image_class' );
function ed_if_featured_image_class($classes) {
 if ( has_post_thumbnail() ) {
 array_push($classes, 'has-featured-image');
 }
 return $classes;
}