Creating the Custom Fields

First, you have to modify your post type function to add a callback. This is the name of the function that will add (and/or remove) custom meta boxes from the Edit screen. Listing 12-21 shows the change.

Listing 12-21. Updating the post_type_courses() function to include callback argument function post_type_courses() { register_post_type( 'course', array(

'supports' => array( 'title', 'editor', ' author' , 'excerpt', 'custom-fields', 'revisions',)

register_taxonomy_for_object_type('college', 'course');

Then you need to add the course_meta_boxes() function, as shown in Listing 12-22. You'll add a whole new section to the Course Post Types plugin to handle the custom meta boxes, and this will be the first part of that section.

Listing 12-22. The callback function that adds meta boxes

function course_meta_boxes() {

add_meta_box( 'course_code_meta', 'normal', 'high' );

add_meta_box( 'instructor_meta',

This function adds two meta boxes, one for the course code and one for the instructor information. The course code box will contain one text field for the code. The instructor box will contain two fields, the name and the e-mail. The arguments of add_meta_box() are:

• Handle: a unique name for this meta box, for your internal use

_('Course Code'), 'course_code_meta_box', 'course', ('Instructor'), 'instructor_meta_box', 'course',

• Callback: the name of the function that will print the contents of the box

• Post type: the names of all the post types that will use this box. To have the box appear on a page as well as a course, you would have used array('course','page') here.

• Section: which part of the Edit screen this box will appear in by default (normal, advanced, or side)

• Priority: how high the box should appear within its section (high, normal, or low)

Next, create the callback functions that print each meta box, as shown in Listing 12-23.

Listing 12-23. Printing the meta boxes function course_code_meta_box() { global $post;

$code = get_post_meta($post->ID, '_course_code', true);

if ( function_exists('wp_nonce_field') ) wp_nonce_field('course_code_nonce',

<label for="_course_code">Course Code (e.g. ENGL I0l)</label> <input type="text" name="_course_code"

value="<?php echo wp_specialchars(stripslashes($code), l); ?>" />

function instructor_meta_box() { global $post;

$name = get_post_meta($post->ID, '_instructor_name', true);

$email = get_post_meta($post->ID, '_instructor_email', true);

if ( function_exists('wp_nonce_field') ) wp_nonce_field('instructor_nonce',

<label for="_instructor_name">Name</label> <input type="text" name="_instructor_name"

value="<?php echo wp_specialchars(stripslashes($name), l); ?>" /> <label for="_instructor_email">Email</label> <input type="text" name="_instructor_email"

value="<?php echo wp_specialchars(stripslashes($email), l); ?>" />

Each function simply prints the form fields within the meta box. To make sure the field values are populated with any previously saved data, call get_post_meta(), which requires three arguments: the post ID, the meta key, and a true/false value determining whether the function should return a single value or all values stored with that key for the post.

The resulting Edit Course screen is shown in Figure 12-22.


Figure 12-22. The Edit Course screen with the new custom fields

In this case, you use meta key names that begin with underscores. If you hadn't, you would see each of these three custom fields in the Custom Fields meta box in addition to your new meta boxes, as shown in Figure 12-23. WordPress will not print meta fields whose keys begin with underscores; they are considered hidden.

Figure 12-23. Duplicated custom fields when keys do not begin with underscores
+1 0

Post a comment