WordPress 3.4 Release Candidate 1

Following a series of beta releases, final testing begins with RC1

http://wordpress.org/news/2012/05/wordpress-3-4-release-candidate/

This is the time when developers, hosts and enthusiast who want a sneak peek into how their sites with the latest release, but don’t want to take part in the core development cycle, really really should test.

Release candidates are like being given a pre-release. It is stable. It has been tested. Unless YOU find any issues, this will be the next release of WordPress. So get stuck in!

Adding custom styling to WordPress custom post types admin screens

My WordPress projects usually end up using at least 3 custom post types, sometimes more than 10. Together with some extra admin screens (from external plugins[1]), the admin menu gets cluttered. All the various edit post admin screens can look very similar to the untrained eye.

What to do about!? Well, I’m not a designer, but I can certainly kick-start things by providing inspiration to my colleagues. What I need is some way of boldly showing them that we can add some styling to specific post type admin screens.

add_filter( 'admin_body_class', 'tcb_custom_post_type_body_class' );
function tcb_custom_post_type_body_class( $classes ){
  global $post;

  $post_class = get_post_type( $post->ID ) . '_admin';
  if( is_array($classes) ):
    $classes[] = $post_class;
  else :
    $classes .= " {$post_class}";
  endif;

  return $classes;
}

That will add a class to the page’s body tag, which can be targeted with CSS. Here is a rather pathetic example for my albums and artists post types:

.albums_admin{
  background-color: #0f0;
}
.artists_admin{
  background-color: #f00;
}

Good luck with that!

Turn your taxonomy into radio select using WordPress walker function

There are a number of ways in which the WordPress taxonomy system could be extended. One such thing would be to restrict the selection of a taxonomy’s terms to just one term per post. To give a simple example, imagine you wanted to represent some books in some libraries (they are reference books only). Create a book custom post type, and assign it a custom taxonomy library. A book can only ever be in one library.

There are 3 steps to creating radio selectors for your library meta box on the admin screen:

  1. Remove the current meta box and hook up your own
  2. Write your new meta box function
  3. Write a class to extend the walker function for radio boxes
1. Remove core default meta box, and hook in your own.

Note: the meta WordPress hook names and HTML class names differ, depending on whether the taxonomy is a tag or a category (hierarchical).

add_action( 'add_meta_boxes', 'tcb_mb_taxonomy_walkers' );
function tcb_mb_taxonomy_walkers( $post_type ) {
  remove_meta_box( 'tagsdiv-library', $post_type, 'side' );
  //remove_meta_box( 'library_typesdiv', $post_type, 'side' ); // Category syntax

  if( $post_type == 'book' ) :
    add_meta_box( 'tagsdiv-library', 'My Books', 'tcb_mb_library', $post_type, 'side' );
    //add_meta_box( 'library_typesdiv', 'My Books', 'tcb_mb_library', $post_type, 'side' );
  endif;
}

2. Write the new meta box function. Making sure you pass in your new walker into the checklist function.

function tcb_mb_library( $post, $meta_box ) {
  $taxonomy = 'library';
  $tax      = get_taxonomy( $taxonomy );
  $selected = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

  echo '<div id="taxonomy-' . $taxonomy . '" class="categorydiv">';
  echo '<input type="hidden" name="tax_input[' . $taxonomy . '][]" value="0" />';
  echo '<ul id="' . $taxonomy . 'checklist" class="list:' . $taxonomy . ' categorychecklist form-no-clear">';
  $walker = new tcb_Walker_Category_Radiolist;
  wp_terms_checklist( $post->ID, array(
    'taxonomy'      => $taxonomy,
    'selected_cats' => $selected,
    'checked_ontop' => false,
    'walker'        => $walker,
  ) );
  echo '</ul>';
}

3. Extend the core walker class into your own

/** Radio Button Walker */
class tcb_Walker_Category_Radiolist extends Walker {
  var $tree_type = 'category';
  var $db_fields = array( 'parent'=>'parent', 'id'=>'term_id' );

  function start_lvl( &$output, $depth, $args ){
    $indent  = str_repeat( "\t", $depth );
    $output .= "$indent<ul class='children'>\n";
  }

  function end_lvl( &$output, $depth, $args ){
    $indent  = str_repeat( "\t", $depth );
    $output .= "$indent</ul>\n";
  }

  function start_el( &$output, $category, $depth, $args ){
    extract( $args );
    if ( empty($taxonomy) )
      $taxonomy = 'category';

    if( $taxonomy == 'category' ) :
      $name = 'post_category';
    else :
      $name = 'tax_input[' . $taxonomy . ']';
    endif;

$output .= "\n
<ul>
	<li id="{$taxonomy}-{$category->term_id}">" . '<label class="selectit"><input value="' . $category->name . '" type="radio" name="'.$name.'" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';</li>
</ul>
  }

  function end_el( &$output, $category, $depth, $args ){
    $output .= "</li>\n";
  }
}

WordPress is more than a CMS, it is a PHP Framework

First of all, let us briefly discuss what a Software Library is

A software library is a collection of resources used to develop software. 

Programmers write functions and classes, these are sets of instructions and defined behaviours. They range from the simple to the complex, and each is a resource that can be repeatedly used to build something bigger. Such a collection of resources is a Software Library.

Software Framework

A Software Framework is a software library with certain goals in mind:

  • The framework code itself is not meant to be edited or changed.
  • Frameworks are extensible. Resources can be specialised or overridden.
  • Frameworks have a default behaviour.
  • The framework dictates how it’s set of resources is used.
Hopefully theme and plugin developers will be familiar enough with WordPress to see where I am going with this. Here is a list of items that one should consider when building themes and plugins for WordPress:
  • Don’t edit core files (your changes will get wiped in the next release).
  • WordPress has filters, hooks and pluggable functions.
  • WordPress comes out of the box as a full working CMS.
  • Use action and filter hooks inside plugins and themes, using codex standards.
WordPress Framework Conclusion
From this, I conclude that WordPress is a framework. It provides us with a library of resources that allow us to extend and specialise it into the site and CMS that we want.