Plugin Developed to Eliminate Hard Coded Theme Values
Wednesday, August 6th, 2008To hard code or hard coding (also, hard-code/hard-coding, hardcode/hardcoding) refers to the software development practice of embedding input or configuration data directly into the source code of a program or other executable object, or fixed formatting of the data, instead of obtaining that data from external sources or generating data or formatting in the program itself with the given input. (According to Wikipedia)
During the creation of UrbanMilwaukee.com one of the obstacles we faced with utilizing the BranfordMagazine theme was the use of hard coded values to control the layout of the site. Specifically this value was used by the theme to determine which articles appeared in the “Featured” section and which categories should display in the two article columns on the homepage. I’ve written code for years and one of the oldest rules in programming is to avoid hard coding of values. The reasons to avoid hard coding are numerous but in this case the hard coded values limited the theme’s flexibility, added to the possibility of errors, required the mapping of a code to a category name in multiple locations, and made configuring the site difficult
To eliminate these issues and allow the site to be more flexible we decided it would be best if we could manage the categories through the Wordpress Administration Panel. To do this we needed to develop a plugin which would allow us to set the categories as needed. Due to time constraints the plugin was designed with a limited number of options which is a design limitation in itself but this design resolved the flexibility and configuration problems we were facing.
The code utilizing the hard coded values that set the “Feature” is located in ui.tabs.php. The line of code is below:
query_posts('showposts=1&cat=1');
This was replaced with the code below
query_posts('showposts=1&cat=' .get_option('urban_mag_config_lead_category1'));
To store the option ‘urban_mag_config_lead_category1′ the plugin needed a dropdown to select the Category. The code for this is below:
$categories = get_categories();
$catid = get_option('urban_mag_config_lead_category1');
wp_dropdown_categories('hierarchical=1&name=urban_mag_config_lead_category1&selected=' . $catid);
This was just one example of the hard coded values that we replaced with dropdown settings in the plugin. By making these changes we are now able to change the featured category whenever needed. Additionally with these changes in place instead of code changes when we setup a new Wordpress site utilizing this theme it will only require configurations. This is just one small example of the changes we’ve made to the BranfordMagazine. In future blogs I’ll continue to highlight other features and enhancements to hopefully give you some ideas for your next theme. And of course, if you are interested in a Wordpress site, drop us a line and let’s talk.

Posts