Created
April 1, 2014 17:58
-
-
Save fleeting/9919477 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* Reviews Custom Post Type | |
* | |
* @since MB 1.0 | |
*/ | |
function mb_cpp_reviews_init() { | |
$field_args = array( | |
'labels' => array( | |
'name' => __( 'Reviews' ), | |
'singular_name' => __( 'Reviews' ), | |
'add_new' => __( 'Add New Review' ), | |
'add_new_item' => __( 'Add New Review' ), | |
'edit_item' => __( 'Edit Review' ), | |
'new_item' => __( 'Add New Review' ), | |
'view_item' => __( 'View Review' ), | |
'search_items' => __( 'Search Reviews' ), | |
'not_found' => __( 'No reviews found' ), | |
'not_found_in_trash' => __( 'No reviews found in trash' ) | |
), | |
'public' => true, | |
'show_ui' => true, | |
'capability_type' => 'post', | |
'hierarchical' => true, | |
'rewrite' => true, | |
'menu_position' => 20, | |
'supports' => array('title', 'thumbnail', 'page-attributes') | |
); | |
register_post_type('reviews',$field_args); | |
} | |
add_action( 'init', 'mb_cpp_reviews_init' ); | |
/** | |
* Tweaks to Featured Image on Reviews | |
* | |
* Moves Featured Image field from sidebar to main on reviews and renames it. | |
* | |
* @since MB 1.0 | |
*/ | |
function mb_cpp_reviews_move_image_box() { | |
remove_meta_box( 'postimagediv', 'reviews', 'side' ); | |
add_meta_box('postimagediv', __('Review Image'), 'post_thumbnail_meta_box', 'reviews', 'normal', 'high'); | |
} | |
add_action('do_meta_boxes', 'mb_cpp_reviews_move_image_box'); | |
/** | |
* Reviews Custom Fields | |
* | |
* @since MB 1.0 | |
*/ | |
function mb_cpp_reviews_metaboxes() { | |
add_meta_box('mb_cpp_reviews_meta', 'Review Information', 'mb_cpp_reviews_meta', 'reviews', 'normal', 'default'); | |
} | |
add_action( 'add_meta_boxes', 'mb_cpp_reviews_metaboxes' ); | |
function mb_cpp_reviews_meta() { | |
global $post; | |
// Noncename needed to verify where the data originated | |
echo '<input type="hidden" name="reviews_meta_noncename" id="reviews_meta_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />'; | |
// Get the data if there is any. | |
$reviewtext = get_post_meta($post->ID, '_reviewtext', true); | |
// Echo out the field | |
echo '<h2 style="margin-bottom: 0;">Review</h2>'; | |
echo '<textarea name="_reviewtext" class="widefat" style="height: 100px;">' . $reviewtext . '</textarea>'; | |
// Get the data if there is any. | |
$sourcename = get_post_meta($post->ID, '_sourcename', true); | |
// Echo out the field | |
echo '<h2 style="margin-bottom: 0;">Source Name</h2>'; | |
echo '<input type="text" name="_sourcename" class="widefat" value="' . $sourcename . '" />'; | |
// Get the data if there is any. | |
$sourceurl = get_post_meta($post->ID, '_sourceurl', true); | |
// Echo out the field | |
echo '<h2 style="margin-bottom: 0;">Source URL (including http://)</h2>'; | |
echo '<input type="text" name="_sourceurl" class="widefat" placeholder="http://" value="' . $sourceurl . '" />'; | |
// Get the data if there is any. | |
$featuredreview = get_post_meta($post->ID, '_featuredreview', true); | |
if($featuredreview == 1) | |
$featuredChecked = ' checked="checked"'; | |
else | |
$featuredChecked = ''; | |
// Echo out the field | |
echo '<h2 style="margin-bottom: 0;">Featured?</h2>'; | |
echo '<label><input type="checkbox" name="_featuredreview" value="1"'.$featuredChecked.'> Feature on homepage</label>'; | |
} | |
function mb_cpp_reviews_save_meta($post_id, $post) { | |
// verify this came from the our screen and with proper authorization, | |
// because save_post can be triggered at other times | |
if ( !wp_verify_nonce( $_POST['reviews_meta_noncename'], plugin_basename(__FILE__) )) { | |
return $post->ID; | |
} | |
// Is the user allowed to edit the post or page? | |
if ( !current_user_can( 'edit_post', $post->ID )) | |
return $post->ID; | |
// OK, we're authenticated: we need to find and save the data | |
// We'll put it into an array to make it easier to loop though. | |
$meta['_reviewtext'] = $_POST['_reviewtext']; | |
$meta['_sourcename'] = $_POST['_sourcename']; | |
$meta['_sourceurl'] = $_POST['_sourceurl']; | |
$meta['_featuredreview'] = $_POST['_featuredreview']; | |
// Add values of $meta as custom fields | |
foreach ($meta as $key => $value) { // Cycle through the $meta array! | |
if( $post->post_type == 'revision' ) return; // Don't store custom data twice | |
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely) | |
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value | |
update_post_meta($post->ID, $key, $value); | |
} else { // If the custom field doesn't have a value | |
add_post_meta($post->ID, $key, $value); | |
} | |
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank | |
} | |
} | |
add_action('save_post', 'mb_cpp_reviews_save_meta', 1, 2); // save the custom fields |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment