All Together

The complete Course Post Types plugin is shown in Listing 12-25. It registers the course post type, adds the college taxonomy, displays the college taxonomy on the Edit Courses screen, and adds the course code, instructor name, and instructor e-mail meta boxes to the Courses ^ Edit screen.

Listing 12-25. The complete Course Post Types plugin

Plugin Name: Course Post Types

add_action('init', 'post_type_courses');

function post_type_courses() { register_post_type( 'course', array(

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

register_taxonomy_for_object_type('college', 'course');

add_action('init', 'create_course_series_tax'); register_activation_hook( _FILE_, 'activate_course_series_tax' );

function activate_course_series_tax() { create_course_series_tax(); $GLOBALS['wp_rewrite']->flush_rules();

function create_course_series_tax() { register_taxonomy( 'college', 'course', array(

add_action( 'save_post', 'save_course_meta_data' );

function course_meta_boxes() {

add_meta_box( 'course_code_meta', __('Course Code'), 'course_code_meta_box', 'course', 'normal', 'high' );

add_meta_box( 'instructor_meta', _('Instructor'), 'instructor_meta_box', 'course',

function course_code_meta_box() { global $post;

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

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

value="<?php echo wp_specialchars(stripslashes(get_post_meta($post->ID, '_course_code', true)), 1); ?>" />

function instructor_meta_box() { global $post;

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(get_post_meta($post->ID, '_instructor_name', true)), 1); ?>" />

<label for="_instructor_email">Email</label> <input type="text" name="_instructor_email"

value="<?php echo wp_specialchars(stripslashes(get_post_meta($post->ID, '_instructor_email', true)), 1); ?>" />

function save_course_meta_data( $post_id ) { global $post;

// ignore autosaves if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; // check nonces check_admin_referer('course_code_nonce', '_course_code_nonce'); check_admin_referer('instructor_nonce', '_instructor_nonce');

// check capabilites if ( 'course' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) ) return $post_id;

// save fields if (empty($_POST['_course_code'])) {

$storedcode = get_post_meta( $post_id, '_course_code', true );

delete_post_meta($post_id, '_course_code', $storedcode);

else update_post_meta($post_id, '_course_code', $_POST['_course_code']);

$storedname = get_post_meta( $post_id, '_instructor_name', true ); delete_post_meta($post_id, '_instructor_name', $storedname);

else update_post_meta($post_id, '_instructor_name', $_POST['_instructor_name']);

$storedemail = get_post_meta( $post_id, '_instructor_email', true ); delete_post_meta($post_id, '_instructor_email', $storedemail);

else update_post_meta($post_id, '_instructor_email', $_POST['_instructor_email']);

add_filter("manage_edit-course_columns", "course_taxonomy_columns");

// rearrange the columns on the Edit screens function course_taxonomy_columns($defaults) {

// preserve the default date and comment columns $comments = $defaults['comments']; $date = $defaults['date'];

// remove default date and comments unset($defaults['comments']); unset($defaults['date']);

// insert college taxonomy column $defaults['colleges'] = __('Colleges');

// restore default date and comments $defaults['comments'] = $comments; $defaults['date'] = $date;

return $defaults;

add_action("manage_pages_custom_column", "course_taxonomy_custom_column"); // for non-hierarchical content types, use the following instead: // add_action("manage_posts_custom_column", "course_taxonomy_custom_column");

// print the college taxonomy terms, linked to filter the posts to this taxonomy term only function course_taxonomy_custom_column($column) { global $post;

$colleges = get_the_terms($post->ID, 'college'); if (!empty($colleges)) {

$thecolleges = array(); foreach ($colleges as $college) { $thecolleges[] = '<a href="edit.php?post_type=course&college='.$college->slug.'>' .

esc_html(sanitize_term_field('name', $college->name, $college->term_id, 'college', 'display')) . "</a>";

Once you've revised the courses to take advantage of the new fields, you also need to modify the single-course.php template. Listing 12-26 shows a revised template that displays the courses in a table, and Figure 12-24 shows the result.

Listing 12-26. The new course archive template using custom fields

<div id="container">

<div id="content">

<?php if ($wp_query->have_posts() ) : ?> <table>

<th>Instructor</th>

<tr id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<td><?php echo get_post_meta($post->ID, '_course_code', true); ?></td> <td><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></td>

<td><a href="mailto:<?php echo get_post_meta($post->ID, '_instructor_email', true); ?>"><?php echo get_post_meta($post->ID, '_instructor_name', true); ?></a></td>

</div><!-- #content --> </div><!-- #container -->

<?php get_sidebar(); ?> <?php get_footer(); ?>

WordPress

Just another WordPress weblog

0 0

Post a comment