Staff Post Type
/*
* Staff Custom Post Type
*/
// Register Custom Post Type: Staff
[add_action](http://codex.wordpress.org/Function_Reference/add_action)( '[init](http://codex.wordpress.org/Plugin_API/Action_Reference/init)', 'register_staff_post_type', 20 );
function register_staff_post_type() {
$labels = array(
'name' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'Staff', 'my_custom_post','custom' ),
'singular_name' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'Staff', 'my_custom_post', 'custom' ),
'add_new' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'Add Staff', 'my_custom_post', 'custom' ),
'add_new_item' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'Add Staff', 'my_custom_post', 'custom' ),
'edit_item' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'Edit Staff', 'my_custom_post', 'custom' ),
'new_item' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'New Staff', 'my_custom_post', 'custom' ),
'view_item' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'View Staff', 'my_custom_post', 'custom' ),
'search_items' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'Search Staff', 'my_custom_post', 'custom' ),
'not_found' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'No Staff found', 'my_custom_post', 'custom' ),
'not_found_in_trash' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'No Staff found in Trash', 'my_custom_post', 'custom' ),
'parent_item_colon' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'Parent Staff:', 'my_custom_post', 'custom' ),
'menu_name' => [_x](http://codex.wordpress.org/Function_Reference/_x)( 'Staff', 'my_custom_post', 'custom' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'Custom Staff',
'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => [get_stylesheet_directory_uri](http://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri)() . '/favicon.ico',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => true,
'query_var' => true,
'can_export' => true,
'public' => true,
'has_archive' => 'staff',
'capability_type' => 'post'
);
[register_post_type](http://codex.wordpress.org/Function_Reference/register_post_type)('staff', $args); //max 20 characters cannot contain capital letters and spaces
}
// Custom Meta Boxes for Student Work
function staff_meta_boxes_setup() {
// [wp](http://codex.wordpress.org/Plugin_API/Action_Reference/wp)'s [add_meta_boxes](http://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes) hook
[add_action](http://codex.wordpress.org/Function_Reference/add_action)('[add_meta_boxes](http://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes)', 'staff_add_meta_boxes');
// Enqueue script to collapse topics
[wp_enqueue_script](http://codex.wordpress.org/Function_Reference/wp_enqueue_script)('hack_view_link', [get_template_directory_uri](http://codex.wordpress.org/Function_Reference/get_template_directory_uri)() . '/js/hack-view-link.js', 'jquery', false, true);
}
// Defines meta box properties
function staff_add_meta_boxes() {
// Main meta box
[add_meta_box](http://codex.wordpress.org/Function_Reference/add_meta_box)(
'Staff',
'Staff Info',
'display_staff_meta',
'staff',
'normal',
'high',
'make_tinymce'
);
}
// Meta fields array
$prefix = 'staff_';
$staff_meta_fields = array(
array(
'label' => 'Title',
'desc' => 'i.e. "Designer"',
'id' => $prefix.'title',
'type' => 'text'
),
);
// Callback from [add_meta_box](http://codex.wordpress.org/Function_Reference/add_meta_box)
function display_staff_meta() {
// Declare our fields array & post vars global so we have access to them
global $staff_meta_fields, $post;
// [wp](http://codex.wordpress.org/Plugin_API/Action_Reference/wp) Nonce for verfication
echo '<input type="hidden" name="staff_meta_box_nonce" value="'.[wp_create_nonce](http://codex.wordpress.org/Function_Reference/wp_create_nonce)(basename(__FILE__)).'">';
echo '<table class="form-table">';
foreach($staff_meta_fields as $field) {
// If we already have a value set grab it for the current field
$info = [get_post_meta](http://codex.wordpress.org/Function_Reference/get_post_meta)($post->ID, $field['id'], true);
echo '<tr><th><label for="'.$field['id'].'">'.$field['label'].'</label>';
echo '<br><span class="description">('.$field['desc'],')</span></th>';
echo '<td>';
switch($field['type']) {
case 'text':
echo '<input type="'.$field['type'].'" id="'.$field['id'].'" name="'.$field['id'].'" value="'.$info.'" style="width:100%">';
break;
case 'editor':
[the_editor](http://codex.wordpress.org/Plugin_API/Filter_Reference/the_editor)([get_post_meta](http://codex.wordpress.org/Function_Reference/get_post_meta)($post->ID, 'staff_content', true), $field['id']);
break;
case 'textarea':
echo '<textarea style="width: 100%; min-height: 150px; resize: none;" id="'.$field['id'].'" name="'.$field['id'].'">'.$info.'</textarea>';
break;
case 'select':
$pages = [get_pages](http://codex.wordpress.org/Function_Reference/get_pages)(array('include' => 12));
echo '<select id="'.$field['id'].'" name="'. $field['id'].'">';
foreach($pages as $page){
echo '<option value="'.$page->ID.'" ', $page->ID == $info ? ' [selected](http://codex.wordpress.org/Function_Reference/selected)="[selected](http://codex.wordpress.org/Function_Reference/selected)"' : '', '>'.$page->post_title.'</option>';
}
echo '</select>';
break;
case 'checkbox':
echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" value="1" ', $info ? ' [checked](http://codex.wordpress.org/Function_Reference/checked)="[checked](http://codex.wordpress.org/Function_Reference/checked)"' : '', '> On';
break;
}
echo '</td></tr>';
}
echo '</table>';
}
function save_staff($post_id) {
global $staff_meta_fields;
// Verify our nonce
if (isset($_POST['staff_meta_box_nonce']) && ($_POST['staff_meta_box_nonce'], basename(__FILE__))) return $post_id;
// Check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
// Check user permissions
if ('page' == $_POST['post_type']) {
if (('edit_page', $post_id)) return $post_id;
} elseif(('edit_post', $post_id)) {
return $post_id;
}
// Loop through fields and save the data
foreach ($staff_meta_fields as $field) {
$old = [get_post_meta](http://codex.wordpress.org/Function_Reference/get_post_meta)($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
[update_post_meta](http://codex.wordpress.org/Function_Reference/update_post_meta)($post_id, $field['id'], $new);
} elseif('' == $new && $old) {
[delete_post_meta](http://codex.wordpress.org/Function_Reference/delete_post_meta)($post_id, $field['id'], $old);
}
}
}
if($current_user->roles[0] != 'author') {
// [wp](http://codex.wordpress.org/Plugin_API/Action_Reference/wp) [add_action](http://codex.wordpress.org/Function_Reference/add_action) hooks to call our functions
[add_action](http://codex.wordpress.org/Function_Reference/add_action)('load-post.php', 'staff_meta_boxes_setup');
[add_action](http://codex.wordpress.org/Function_Reference/add_action)('load-post-new.php', 'staff_meta_boxes_setup');
[add_action](http://codex.wordpress.org/Function_Reference/add_action)('[save_post](http://codex.wordpress.org/Plugin_API/Action_Reference/save_post)', 'save_staff');
}