WordPress Wednesday: Custom Field Templates Part II – Using PHP in Your Templates

Geek Factor: 5

In the last WordPress Wednesday, we talked about the Custom Field Template plugin. This week, we’re going to go into a little more detail about this plugin, which was developed by Hiroaki Miyashita; specifically, we’re going to talk about how to incorporate PHP into the custom field templates that you build.

Let’s start with the Lawblogs.ca example mentioned in part one — specifically, our ‘Clawbies Year’ checkbox list. On Lawblogs.ca, this is used to indicate which years a blog has won a Clawbie award:

A basic way to set up the Custom Field Template for these checkboxes would be like this:

[html] [Clawbies Year] type = checkbox values = 2006 # 2007 # 2008 # 2009 # 2010 # 2011 # 2012 # 2013 [/html]

This code works great, and it’s obviously very simple to set up. The downside is that I will have to remember to update it each year to keep the list usable. Obviously, it’s not a hardship to type a hashmark followed by four numbers every 12 months, but this is where using PHP with Custom Field Templates can be super handy.

In the ‘PHP Code (Experimental Option)’ panel of the plugin, you can create PHP that generates the values for different Custom Field Template options.

For example, to have our years automatically update, we can create a loop that goes from our starting year (in this case, 2006) and ends on the current year (which we can get using the PHP date() function. Using the PHP range() function, we can generate a value for each of the years between 2006 and the current year, 2013. Lastly, we can put that range in a foreach() loop, and store each year value in the $values[] array, which will be used by the Custom Field Template plugin:


Each PHP Code you include is assigned an ID based on the text box it’s entered into. Above in the screenshot, the PHP code has been entered into a textarea titled Code #0 — each additional PHP Code textarea has its own ID number. This is how you assign that particular code to one of the Custom Field Templates.

Using the ‘Clawbies Year’ example from above, we would replace the written-out ‘values’ with which PHP ‘code’ we’d like to use:

[html] [Clawbies Year] type = checkbox code = 0 [/html]

Voila! Now our ‘Clawbies Year’ checkboxes will always be up-to-date.

1,200 words seems a bit excessive for a post about generating eight numbers.

Fortunately, this isn’t the end of what can be done with the PHP Code feature. One of the most powerful tricks I’ve found involves pairing WordPress queries with the simple PHP Code example above to create dynamically updating dropdowns and lists.

One common example we use often on websites at Stem is querying pages from different sections of the site. For example, when building a law firm website, it may be required to list the different practice groups that each firm member belongs to. In this example, we’re going to assume the fairly-standard set up of having the practice area pages share the same common parent page.

Using PHP in the Custom Field Template, you can first create a query of all of the practice pages on the site.

[php] //query to get practice area pages $posts = get_posts(array( 'post_type' => 'page', 'post_parent' => 123, //update to match the Practice Area parent page ID 'numberposts' => -1, // grabs all of the subpages 'orderby' => 'menu_order', 'order' => 'ASC')); // foreach loop to store the practice area information // in the arrays used by the Custom Field Template foreach($posts as $post) { $values[] = $post->ID; $valueLabels[] = $post->post_title; } [/php]

The above code would go into a separate textarea under PHP Code than the first example — although the Custom Field Template PHP Code section only starts with one textarea, once you add text to it a new one is added; this continues as you edit each new textarea:


In this example, we’re using the $values[] array, which stores the IDs of the practice area pages. We’re also using the $valueLabels[] array — this one isn’t necessarily required, but it makes it possible to display a different label with each option than the actual value. In this case it’s very helpful, as the values we’re storing are the page IDs (I don’t know about you, but I don’t often memorize the IDs of every page on sites I build). This way, we can store the IDs as the values, but display the practice area page titles with the checkboxes, so it’s clear which page is which.

The PHP Code can then be used in a similar fashion as the ‘Clawbies Year’ example above — you could also use radio or select inputs for single select options. Note that the code below matches the ‘Code #1’ of the textarea the Practice Area PHP code is stored in:

[html] [Practice Areas] type = checkbox code = 1 [/html]

The output for this Custom Field Template would appear on the lawyer profile pages as follows:


Best of all? Each time a practice area is added to the site, this list will be automatically updated to include it on every profile that uses this template.

Because we’ve set the ‘value’ of each checkbox as the page’s ID (rather than the page’s name), we can do more than display the practice area names — we can include links, or a quick excerpt — anything that could be displayed with a regular WP_Query().

Here is a simple example that displays the page’s title and content, followed by an unordered list of the ‘Practice Areas’ checked in the Custom Field Template:


ID, "Practice Areas", false); // check if the $practiceArea variable has any content if($practiceAreas) { ?>

Practice Groups

    $practiceAreas,'posts_per_page'=>'-1','post_type'=>'page','orderby' => 'menu_order','order' => 'ASC')) : // if not an array, use 'p' for a single post ID $practiceAreasQuery = new WP_Query(array('p'=>$practiceAreas,'posts_per_page'=>'-1','post_type'=>'page')); // with the $practiceAreasQuery results, run a Loop while ($practiceAreasQuery->have_posts()) : $practiceAreasQuery->the_post(); ?>

Visually, the output would look something like the following:


Note: You can learn more about WordPress’s get_post_meta() function, used in the above example, in this WordPress Wednesday post, or from the WordPress Codex. The important bit to note in this example is the last parameter value, ‘false’, is for the optional $single parameter. From the Codex:

$single (boolean) (optional) If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields….

Because we are gathering an array of information — different practice area page IDs — we want to store this information in an array. If the information you’re grabbing is better used as a string, then change this value to ‘true’.

Obviously, using PHP to query WordPress posts in the Custom Field Templates can go well beyond pairing lawyers to practice groups on law firm websites. I’ve only started scraping the surface of what’s possible myself; if you can see a use for this kind of functionality in your own project, please share in the comments below!

In the next WordPress Wednesday, we’re going to cover what I think is the final reason that makes this plugin (and the WordPress custom field functionality on the whole) so useful: querying by custom field value.

Leave a Comment

Note: Fields marked with a * are required; email addresses are not published.

Legal FAQ Collections