WordPress Admin Makeover Coming in 3.2

WordPress Admin Preview

I somehow had missed that WordPress 3.2 was going to feature a makeover of the WordPress admin theme. The new theme looks sharp. Here’s a preview. Let’s hope this doesn’t create issues with plugins.

Why WordPress?

I assume other developers often ponder this question. In a practical sense, the answer is “because it’s what you know”. But I’m pondering a bigger question. As a developer with a keen interest in PHP should I waste my time with WordPress? Can you ever get “respect” as a PHP developer when your tool of choice is WordPress?

WordPress is a dinosaur compared to most of the popular custom PHP frameworks. Frameworks like Codeigniter,Kohana and others are very light weight and scalable. They generally focus on concepts like OOP (object-oriented programming) and MVC (model-view-controller). Code reusability and encapsulation are priorities not luxuries.

WordPress is a vestige of  PHP before PHP had a robust object-oriented structure. As such it is mostly a collection of libraries of functions. Of course, with every new release these function are streamlined and improved. But ultimately WordPress is always going to be a procedural framework.

So if you’re interested in PHP why waste your time with a code base that will never be the vangard of PHP development?

Well, for one, just because WordPress core is less than impressive from a PHP point of view, does not mean that cutting edge approaches to coding are not implemented via plugins. Plugins are essentially little custom apps that tie into the WordPress API. But as such there are lots of plugins that exemplify flexible and cutting edge approaches to code development.

For instance, there’s a plugin out there the integrates Doctrine ORM with WordPress. Doctrine is a “Object-Relational-Mapping” interface that aims to cleanup how PHP interacts with Databases. Sure WordPress core doesn’t have a Database Abstraction layer, but if your interested you can use Doctrine in your plugins!

PodsCMS, one of my favorite plugins, is another one that pushes better code into WordPress. Pods is capable of managing massive complexity in WordPress content, but itself is a very light-weight, Object-Oriented framework. Moreover, PodsCMS, while not entirely MVC, at least separates code from markup which is a feature sorely lacking in WordPress core.

Other plugins, among them Shopp (an e-commerce plugin) and GravityForms, take very Object-Oriented approaches to coding. So just because there are limitations to WordPress core, does not mean developers passionate about PHP cannot excel and flourish. If anything, the lack of cutting edge PHP in WordPress creates a tremendous opportunity for hard core PHP developers.

But why? Why drive a station wagon when you could be driving a sport car? Well, because you have places to go and things to do … and people to take with you. The simple reality is that there’s a huge market for WordPress development. A bigger market than just about any other out-of-the-box CMS. And that means an opportunity develop good code for good projects.

So if you’re a WordPress developer and want to branch out into custom PHP, my advice is first, learn the WordPress way of doing things. Then forget the WordPress way of doing things. Experiment with custom frameworks and try to find ways to incorporate the innovations into the WordPress projects you’re working on.

And most importantly, don’t be afraid to NOT use WordPress for a project or two. Getting outside your comfort zone is what gets your  creative juices flowing.

Genesis Framework, Not Too Shabby

Screen shot 2011-05-02 at 10.13.12 AM

So, I’ve posted elsewhere that I’m not a huge fan of theme frameworks. The short version: I’m not sure we need anymore hooks and filters than WordPress Core already provides and my time is better spent learning those hooks than learning some trendy theme hooks that merely obscur or repackage those core WP hooks.

However, then I contradicted myself and went a redesigned my website using the Genesis Framework. So I feel obligated to explain. I chose the Genesis Framework because I like StudioPress. I first used a Brian Gardner theme back in 2008 when it was still “Revolution”. I’ve always found his code clean, efficient and easy to work with. Indeed, I found those traits reflected in Genesis as well.

Moreover, modifying Genesis was (for the most part) much more straight forward than other frameworks like Thesis. For instance, with Genesis you can still use custom template pages without putting all the code into a function. I’m old school so I like that. I also find the core Genesis libraries are intuitively organized so that if you want to know what hooks are involved in a particular piece of the header,  it’s relatively easy to find ‘/lib/structure/header.php’.

So, while in general, I still don’t dig on theme frameworks, Genesis is definitely a good one. Cheers, Brian.

Add tag-like functionality to your pod inputs

Screen shot 2011-04-19 at 10.04.29 AM

Yet another update: The code below expects the name of your pick field to correspond to the name of the related pod type ( i.e. a pickfield name “genre” should relate to a pod called “genre” , as opposed to “file_type”).

Warning: My code highlighter is gimping up the code a little. If you plan to copy/paste, please download the text file instead of copying straight from the webpag.

Try the DEMO
(login using guest/guest).

Update: The demo was down yesterday because I was playing with some code. Sorry about that.

Update 2:
Also note that if this input helper isn’t working on your site make sure that you’ve enqueued the “suggest” script which ships automatically with WordPress. Do this by adding the following line of code to your functions.php file:

if (!wp_script_is('suggest', 'queue') && !wp_script_is('suggest', 'to_do') && !wp_script_is('suggest', 'done'))  {
        wp_enqueue_script('suggest');
}

So this isn’t really a helper so I’m not going to add it to the PodsCMS helper gallery. However, I think it’s a useful bit of code for pods developers.

The problem: You prefer using pods for developing content that needs lots of relationships managed seamlessly. Post types and tags just won’t cut it. But man, you miss that slick tag UI that WordPress has where you just start typing, hit enter and POW the tag is added. Pods doesn’t have anything like this natively.

The solution:

First create a new input helper and paste in the following code:


Add
x  

This helper creates an input field and calls up some jQuery magic to make it auto-complete. But in order for this magic to work you need to add a function to your functions.php (or a plugin file if that’s the case) and hook it to the wp_ajax_{$action} hook. For more about this hook check Gary Cao’s article.

add_action('wp_ajax_pods_suggest','ajax_pod_search');
function ajax_pod_search() {
	if(isset($_GET['q'])) {
		$str = $_GET['q'];
		if(strlen($str) < 2) { die; }
			$pod = $_GET['datatype'];
			global $wpdb;
			$query = $wpdb->prepare("SELECT name FROM {$wpdb->prefix}pod_tbl_{$pod} WHERE name LIKE '%s'", '%'.like_escape($str).'%');
			$results = $wpdb->get_col($query);
			echo join( $results, "\n" );
		}
	die;
}

Now simply deploy the input_helper on your chosen multi-pick field pod.

But wait! It’s not saving your pod items. That’s because PodsCMS expects to receive a list of comma-separated pod ids to register the relationship. But our jQuery is outputting the name instead.

So we now need to add a pre-save helper to manage migrating tag names to ids.

save_pod_item(array('datatype'=>$field, 'columns' => array('name'=>$name)));
	} else {
		$pid = (is_array($pid)) ? $pid[0] : $pid;
	}
	$vals[] = $pid;
endforeach;
} else {
	$pid = pod_check($field, $name, 'name');
	if(!$pid)
	{
	  $new_tag = new PodAPI();
	  $pid = $new_tag->save_pod_item(array('datatype'=>$field, 'columns' => array('name'=>$name)));
	} else {
		$pid = (is_array($pid)) ? $pid[0] : $pid;
	}
	$vals = $pid;
}
$val_str = implode(',',$vals);
$columns[$field]['value'] = $val_str;
endif;
?>

Note that we have to tell the helper the field to which we want it to apply. We do this by setting the $field variable. If you are going to use the input helper on multiple fields, you’ll want to create a pre-save helper for each field. This helper will iterate over the submitted pod names, check to see if that pod exists already and getting its id. If the pod name does not already exist, the helper will create it.

Note that this helper makes use of a function called “pod_check”. You will need to add this function to your functions.php file as well.

function pod_check($datatype, $value='', $field='id') {
    global $wpdb;
    $datatype = pods_sanitize($datatype);
    $field = pods_sanitize($field);
    $return = $wpdb->get_col($wpdb->prepare("SELECT `id` FROM `{$wpdb->prefix}pod_tbl_{$datatype}` WHERE `$field` = ".('id'==$field?"%d":"%s"),array($value)));
    if (true == $return && 0 < $return) {
    	return $return;
    } else {
    	return false;
    }
}

This function actually does the work of looking up the pod id.

And that's it. You can make any pod into a "tag" using this approach. But keep in mind that if you are using this field to relate a pods with required fields, you will need to modify the pre-save helper to add those required fields.

WordPress Patching

So I’m very excited that I submitted my first patch to the WordPress core this morning. Here’s hoping it’s accepted !!!!

Having Trouble with SSL?

This plugin helped.

WordPress 3.1: Remove the Admin Bar

Wordpress Admin Bar

WordPress Admin Bar Disable
Within a week of WordPress 3.1 being released there are already half a dozen plugins available to remove the fancy schmancy admin bar. I like this one the best. The key is that it lets you disable the admin bar based on roles AND capabilities. Very handy if you’re running a network site or a site with lots of user roles.

WordPress 3.1: Query Multiple Custom Fields

So I just implemented for the first time the new WordPress ‘meta_query’ structure that was added to WordPress 3.1. I must say, it works swimmingly. Here’s my query:

-> To look up events that start after the current date and then order those events ascending by starting date:

		$tday = date('Y-m-d H:i:s');
		$args = array(
			'post_type' => 'events',
			'showposts' => 1,
			'meta_query'=> array(
				array(
					'key'=>'simplr_starts',
					'value'=> $tday,
					'compare' => '>'
				)
			),
			'meta_key'=>'simplr_starts',
			'orderby'=>'meta_value',
			'order'=>'ASC'
		);
		$events = new WP_query($args);
// run the rest of the loop as usual.

Check out Scribu for more info.

WordPress Hack #2: Protect Your Admin

Ever wanted to protect your admin area from access by non-admin users? Try adding this to your functions.php.

add_action('auth_redirect','custom_admin_access');
function custom_admin_access() {
global $current_user;
   if( !current_user_can('manage_options') && is_admin() ) {
      wp_redirect( home_url() );
   }
}

WordPress jQuery PopUp Plugin (BETA)

Status: Due to my being overworked at the moment, I am only “sort of” supporting this plugin. If you need full time support I recommend Popup Domination

Nothing is more exciting than when a clients needs collides with a developers interests. I’ve been wanting to write this plugin for a while but never had the time to make it a priority … at least not until this week when a client requested. Synergy! I started writing it this morning and couldn’t stop until I had something releasable.

WordPress jQuery PopUp Plugin

So here is is. Just upload and activate.

Download it!