Created
January 10, 2012 16:37
-
-
Save thekevinscott/1589923 to your computer and use it in GitHub Desktop.
Easily add custom post types, with custom fields, in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
//////////////////////////////////////////////////////////// | |
////// | |
////// Abstracted code for quickly adding custom post types | |
////// include this file in functions.php, then add custom post types like so: | |
////// | |
//////////////////////////////////////////////////////////// | |
$custom_post_types = array( | |
'production' => array( | |
'options' => | |
array( | |
'labels' => array( | |
'name' => __( 'Productions' ), | |
'singular_name' => __( 'Production' ) | |
), | |
'supports' => array('title','author','editor','thumbnail','excerpt','revisions'), | |
'public' => true, | |
'has_archive' => true, | |
), | |
'custom_fields' => array( | |
'buy_tickets' => array('label'=>'Buy Tickets'), | |
'buy_tickets_link' => array('label' => 'Buy Ticket Link'), | |
'learn_more' => array('label'=> 'Learn More'), | |
'learn_more_link' => array('label' => 'Learn More Link') | |
) | |
) | |
); | |
*/ | |
add_action( 'init', 'create_custom_post_types' ); | |
function create_custom_post_types() { | |
global $custom_post_types; | |
foreach($custom_post_types as $name=>$custom_post_type) | |
{ | |
register_post_type( $name,$custom_post_type['options']); | |
} | |
} | |
add_action('admin_init','meta_init_add_boxes'); | |
function meta_init_add_boxes() { | |
global $custom_post_types; | |
foreach($custom_post_types as $name=>$custom_post_type) | |
{ | |
add_meta_box( | |
$name.'_meta', | |
'Custom Fields', | |
'post_type_meta', | |
$name, | |
'advanced', | |
'high' | |
); | |
} | |
} | |
//error_reporting(E_ALL); | |
function post_type_meta() { | |
global $post; | |
global $custom_post_types; | |
$post_type = $custom_post_types[$post->post_type]; | |
$custom_fields = $post_type['custom_fields']; | |
$fields = array(); | |
foreach($custom_fields as $field=>$field_info) { | |
$fields[$field] = $field_info; | |
$fields[$field]['value'] = get_post_meta($post->ID,$field,TRUE); | |
} | |
//Call the write panel HTML | |
?> | |
<style type="text/css" media="screen"> | |
.meta_panel .description { | |
display: none; | |
} | |
.meta_panel label { | |
display: block; | |
font-weight: bold; | |
margin: 6px; | |
margin-bottom: 0; | |
margin-top: 12px; | |
} | |
.meta_panel label span { | |
display: inline; | |
font-weight: normal; | |
} | |
.meta_panel span { | |
color: #999; | |
display: block; | |
} | |
.meta_panel textarea, | |
.meta_panel input[type='text'] { | |
margin-bottom: 3px; | |
width: 100%; | |
} | |
.meta_panel h4 { | |
color: #999; | |
font-size: 1em; | |
margin: 15px 6px; | |
text-transform:uppercase; | |
} | |
</style><?php | |
?> | |
<div class="meta_panel"> | |
<?php foreach($fields as $field=>$field_info) : ?> | |
<label><?php echo $field_info['label'] ?></label> | |
<input type="text" name="<?php echo $field ?>" value="<?php echo (!empty($field_info['value'])) ? $field_info['value'] : null; ?>"/> | |
<?php endforeach; ?> | |
<?php wp_nonce_field( plugin_basename( __FILE__ ), $post->post_type.'_noncename' ); ?> | |
</div> | |
<?php echo ''; | |
} | |
add_action( 'save_post', 'save_postdata' ); | |
function save_postdata($post_id) { | |
global $post; | |
global $custom_post_types; | |
if ($post && $post->post_type) { | |
$post_type = $custom_post_types[$post->post_type]; | |
$custom_fields = $post_type['custom_fields']; | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; | |
// Check permissions | |
if ( 'page' == $_POST['post_type'] ) { | |
if ( !current_user_can( 'edit_page', $post_id ) ) return; | |
} else { | |
if ( !current_user_can( 'edit_post', $post_id ) ) return; | |
} | |
foreach($custom_fields as $key=>$field_info) { | |
$custom_field = $_POST[$key]; | |
if(is_null($custom_field)) { | |
delete_post_meta($post_id, $key); | |
} elseif(isset($custom_field)&& !is_null($custom_field)) { | |
update_post_meta($post_id,$key,$custom_field); | |
} else { | |
add_post_meta($post_id, $key,$custom_field, TRUE); | |
} | |
} | |
} | |
return $post_id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment