More WordPress Theme Streamlining

In my last post (nearly five months ago…) I discussed creating constants for things that WordPress loves to run to the database to get.  Now, constants are great, but sometimes you just want to roll everything into an easily-used array and be done with it.  So, for the majority of my theme options, I do just that.  I’m a stickler for naming conventions, because it’s so much easier to deal with after the fact.  Name it properly and give it a prefix, and you shouldn’t have trouble finding it or figuring out what it does a year down the road when you don’t even remember writing it.  When creating theme options for a particular site, I start all of them with a prefix – say, to_ – and then a highly descriptive name.  ‘to_home_page_splash_image’ is pretty self-explanatory.

Prefixing all of my theme options the same allows me to go to the database once and grab them all at the same time:

add_action('after_setup_theme', 'mytheme_lifeshield_setup');

function mytheme_option_setup(){
 global $wpdb;

 $arr_results = $wpdb->get_results("SELECT * FROM $wpdb->options WHERE option_name LIKE 'to_%';");
 $arr_to_options = array();
 foreach($arr_results as $obj_result){
  $arr_to_options[$obj_result->option_name] = $obj_result->option_value;
 $GLOBALS['to_options'] = $arr_to_options;

Now, when I need a particular option, I just reference the global variable: . No need for yet another get_bloginfo() call.