Skip to content

Instantly share code, notes, and snippets.

@fikrirasyid
Created March 8, 2013 12:30
Show Gist options
  • Save fikrirasyid/5116160 to your computer and use it in GitHub Desktop.
Save fikrirasyid/5116160 to your computer and use it in GitHub Desktop.
Meta box theme 27
/*
* --------------------------------------------------------------------------------------------------------------------------------
* CUSTOM META BOXES FOR EASIER CONTENT EDITING
* --------------------------------------------------------------------------------------------------------------------------------
*/
add_action( 'add_meta_boxes', 'wp_meta_boxes_add' );
function wp_meta_boxes_add(){
// Custom Meta Boxes for post-type post
add_meta_box( 'binus-meta-boxes', 'BINUS Meta Information', 'wp_meta_boxes', 'post', 'normal', 'high' );
}
add_action('admin_print_styles', 'wp_admin_css');
function wp_admin_css(){
wp_register_style('binus-admin-style', get_bloginfo('template_directory') . '/css/dashboard.css', array(), false, 'screen');
wp_enqueue_style('binus-admin-style');
}
// Meta box for 'post'
function wp_meta_boxes(){
// $post is already set, and contains an object: the WordPress post
global $post;
$values = get_post_custom( $post->ID );
if (isset($values['display_slide_item'][0])){
$display_slide_item = $values['display_slide_item'][0];
} else {
$display_slide_item = '';
}
if (isset($values['video_url'][0])){
$video_url = $values['video_url'][0];
} else {
$video_url = '';
}
// We'll use this nonce field later on when saving.
wp_nonce_field( 'wp_meta_boxes_nonce', 'nonce_wp_meta_box' );
?>
<p><strong>Slideshow Preferences</strong></p>
<?php
$display_slide_item_options = array(
array('display_normally', 'Display normally, as link-wrapped image'),
array('display_as_plain_image', 'Display as plain image without link'),
array('display_as_video', 'Display slide item as video player (Embedded video)')
);
foreach( $display_slide_item_options as $option ){
if ( $display_slide_item == null || $display_slide_item == '' ){
$display_slide_item = 'display_normally';
}
if ( $display_slide_item == $option[0]){
$status = 'checked="checked"';
} else {
$status = '';
}
echo '<p><input type="radio" name="display_slide_item" class="binus-input-radio" value="'. $option[0] .'" '. $status .' />';
echo '<label for="display_as_plain_image">'. $option[1] .'</label> </p>';
}
?>
<br />
<p>
<label for="video_url"><strong>URL of Video You Want To Embed on Slideshow:</strong></label>
<input type="text" name="video_url" id="video_url" class="input-text" value="<?php echo $video_url; ?>" />
</p>
<?php
}
// Save box for 'post'
add_action( 'save_post', 'wp_meta_boxes_save' );
function wp_meta_boxes_save( $post_id ){
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['nonce_wp_meta_box'] ) || !wp_verify_nonce( $_POST['nonce_wp_meta_box'], 'wp_meta_boxes_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchors can only have href attribute
)
);
// Make sure your data is set before trying to save it
if( isset( $_POST['display_slide_item'] ) )
update_post_meta( $post_id, 'display_slide_item', wp_kses( $_POST['display_slide_item'], $allowed ) );
if( isset( $_POST['video_url'] ) )
update_post_meta( $post_id, 'video_url', wp_kses( $_POST['video_url'], $allowed ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment