Advanced Content Management with WordPress Custom Field  Templates

1/13/2011 – New and improved function used for retrieving custom field data for the current post called base_get_all_custom_fields(). This function will run a database query to get all custom fields attached to the current page, post or post type and store them in an array.

Custom Field Template Result
Custom Field Template Result

WordPress is great for dynamically managing the content of a website, but sometimes you encounter situations that require advanced management to handle different sections of a single page. It’s would be great if we could manage separate chunks of content with more than one WYSIWYG. Well guess what, you can, and it’s easier than you may think.

The best solution I’ve found for this situation is the Custom Field Template plugin. With this plugin you can add multiple WYSIWYG managed areas to one WordPress page or post. You can even use select boxes, radio buttons, and checkboxes to dynamically control the content of a page.

Setup Your Template

  1. Download and activate Custom Field Template
  2. Go to Settings > Custom Field Template
  3. Customize Your Template. The template I’ll be using in this example is simplified and will give you two added WYSIWYG areas. Notice I’ve set the title to Template Title: Additional Content. This will set the custom field title, similar to the way “Discussion” is the title for pingback and comment options.
Custom "Additional Content" Field Collapsed
Custom Additional Content Field Collapsed
Custom "Additional Content" Field Expanded
Custom Additional Content Field Expanded

Template Code

Add this under Settings > Custom Field Template > Custom Field Template Options Box

[cft_tinymce_1]
type = textarea
rows = 8
tinyMCE = true

[cft_tinymce_2]
type = textarea
rows = 8
tinyMCE = true

[cft_checkboxes_1]
type = checkbox
value = option1 # option2 # option3 
custom-field-template-settings
Settings > Custom Field Template
Settings > Custom Field Template
Settings > Custom Field Template

Use The get_custom_field() Function to Get Custom Fields Individually

Open up your theme’s functions.php file. If you don’t have one, create it. Add the following PHP function to the file.

// Get Custom Field Template Values
function get_custom_field($field) {
	global $post;
	$custom_field_data = get_post_meta($post->ID, $field, false);
	if($custom_field_data) {
		if( count($custom_field_data) > 1 ) {
			return $custom_field_data; 
		} else {
			return $custom_field_data[0];
		}
	} else {
		return false;
	}
}

Load The Custom Field Content into Your Theme Individually

Next we need to take the content created by these new fields and add it to our template. To do this we’ll need some basic PHP.

 

Block #1

Block #2

Return Checkbox/Multiple Values Stored in a Single Custom Field

Several people have mentioned that these functions do not work with checkboxes and radio buttons. These fields multiple data entries into a single custom field. I've written the get_custom_field() function to return fields like this as an array. You can display the values like this:

 
/** Get a custom field with multiple values and return as an array */
$checkboxes_1 = get_custom_field('cft_checkboxes_1');
if( $checkboxes_1 ) {
?>

Custom Field (multiple)

Using The base_get_all_custom_fields() Function to Get All Custom Fields in One Swoop

Open up your theme's functions.php file. If you don't have one, create it. Add the following PHP function to the file.

// Get all custom fields attached to a page
if( !function_exists('base_get_all_custom_fields') ) {
	function base_get_all_custom_fields(){
		global $post;
		global $wpdb;
		$sql = "SELECT * FROM $wpdb->postmeta	WHERE post_id = $post->ID ORDER BY meta_id ASC";
		$custom_fields = $wpdb->get_results($sql);
		$custom_field_array = array();
		foreach($custom_fields as $field) {
			$custom_field_array["$field->meta_key"] = $field->meta_value;
		}
		return $custom_field_array;
	}
}

Load The Custom Field Content into Your Theme

Next we need to take the content created by these new fields and add it to a template in your theme. To do this we'll need some basic PHP. Open up a template file, such as page.php, in your WordPress theme and add the following:

 
// Get all custom fields attached to this post and store them in an array
$custom_fields = base_get_all_custom_fields();
?>


Block #1

Block #2

I usually choose to create a custom page template to display these new content blocks. You can easily include these fields on every post, page or both. All you have to do is select an option in the plugin settings page (there's a small radio button labeled "Post Type:").

For the purpose of presentation I've added some basic styles into the mix. Nothing fancy.

Add Some Basic CSS

/* Disposable Styles */
*.content-box {
	padding:12px;
	background:#e5e5e5;
	width:281px;
	float:left;
}
*.content-box h2 {
	margin-top:0;
	padding-bottom:6px;
	border-bottom:1px solid #ccc;
}
#block-2 {
	margin:0 0 0 20px;
	display:inline; /* For IE6 */
}

Want more CMS control? Learn how to add an infinite number of CFT fields with the multiple fields & groups functionality that was recently released in late 2010.

Related Articles

Meet the Author

Kevin Leary, WordPress Consultant

I'm a custom WordPress web developer and analytics consultant in Boston, MA with 17 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.