Skip to content

Instantly share code, notes, and snippets.

@jeremyboggs
Created May 29, 2012 19:06
Show Gist options
  • Save jeremyboggs/2830072 to your computer and use it in GitHub Desktop.
Save jeremyboggs/2830072 to your computer and use it in GitHub Desktop.
Custom post type for People in WP. Includes dropdown of existing users to associate to the person post.
<?php
/**
* Custom Post Type for People
*/
function people_register_post_types() {
register_post_type( 'people',
array(
'labels' => array(
'name' => __( 'People' ),
'singular_name' => __( 'Person' )
),
'capability_type' => 'post',
'public' => true,
'supports' => array( 'title', 'editor', 'thumbnail'),
'menu_position' => 20,
'hierarchical' => false,
'has_archive' => true,
'show_in_nav_menus' => true,
'rewrite' => array('slug' => 'people')
)
);
}
add_action( 'init', 'people_register_post_types' );
/**
* Adds our post meta boxes for the 'sp_workshop' post type.
*/
function people_add_meta_boxes() {
add_meta_box("wp-user-information", __('Personal Info'), "people_meta_box", "people", "advanced", "high");
}
add_action( 'admin_init', 'people_add_meta_boxes');
function people_meta_fields() {
return array('person_title', 'person_email', 'person_phone', 'person_twitter', 'person_url', 'person_user_id');
}
/**
* Meta box for personal information.
*/
function people_meta_box(){
global $post;
$custom = get_post_custom($post->ID);
$fields = people_meta_fields();
?>
<?php foreach ($fields as $field): if ($field == 'person_user_id') continue; ?>
<p><label for="<?php echo $field; ?>"><?php echo ucwords(str_replace('person_', ' ', $field)); ?></label></p>
<p><input type="text" value="<?php echo @$custom[$field][0]; ?>" name="<?php echo $field; ?>" /></p>
<?php endforeach; ?>
<p><label for="person_user_id">User</label></p>
<p><?php wp_dropdown_users(array('name' => 'person_user_id', 'selected' => @$custom['person_user_id'][0])); ?></p>
<?php
}
/**
* Saves our custom post metadata. Used on the 'save_post' hook.
*/
function people_save_post(){
global $post;
$fields = people_meta_fields();
foreach ($fields as $field) {
if ( array_key_exists($field, $_POST)) {
update_post_meta($post->ID, $field, $_POST[$field]);
}
}
}
add_action( 'save_post','people_save_post');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment