<?php
global $meta_box_video;
$meta_box_video = array(
'id' => 'f-meta-box-video',
'title' => __('Video Settings'),
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array( "name" => __('Desciption'),
"desc" => __('Input the video desciption.'),
"id" => "video_desc1",
"type" => "textarea"
)
)
);
add_action('admin_menu', 'add_video_box');
function add_video_box() {
global $meta_box_video;
add_meta_box($meta_box_video['id'], $meta_box_video['title'], 'show_box_video', $meta_box_video['page'], $meta_box_video['context'], $meta_box_video['priority']);
}
function show_box_video() {
global $meta_box_video, $post;
// Use nonce for verification
echo '<input type="hidden" name="f_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box_video['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
switch ($field['type']) {
//If Text
case 'text':
echo '<tr>',
'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style=" display:block; color:#999; margin:5px 0 0 0; line-height: 18px;">'. $field['desc'].'</span></label></th>',
'<td>';
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? htmlentities( $meta ) : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '" size="30" style="width:75%; margin-right: 20px; float:left;" />';
break;
//If textarea
case 'textarea':
echo '<tr>',
'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="line-height:18px; display:block; color:#999; margin:5px 0 0 0;">'. $field['desc'].'</span></label></th>',
'<td>';
echo wp_editor(($meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES))), $field['id'], array('dfw' => true, 'tabfocus_elements' => 'sample-permalink,post-preview', 'editor_height' => 360) );
// echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '" rows="8" cols="5" style="width:100%; margin-right: 20px; float:left;">', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '</textarea>';
break;
//If Select
case 'select':
echo '<tr>',
'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style=" display:block; color:#999; margin:5px 0 0 0;">'. $field['desc'].'</span></label></th>',
'<td>';
echo'<select name="'.$field['id'].'">';
foreach ($field['options'] as $option) {
echo'<option';
if ($meta == $option ) {
echo ' selected="selected"';
}
echo'>'. $option .'</option>';
}
echo'</select>';
break;
}
}
echo '</table>';
}
add_action('save_post', 'f_save_data');
/*-----------------------------------------------------------------------------------*/
/* Save data when post is edited
/*-----------------------------------------------------------------------------------*/
function f_save_data($post_id) {
global $meta_box_video;
// verify nonce
if (!wp_verify_nonce($_POST['f_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($meta_box_video['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], stripslashes($new));
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
/* wordpress 只支持下面几个 post formats
function get_post_format_strings() {
$strings = array(
'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
'aside' => _x( 'Aside', 'Post format' ),
'chat' => _x( 'Chat', 'Post format' ),
'gallery' => _x( 'Gallery', 'Post format' ),
'link' => _x( 'Link', 'Post format' ),
'image' => _x( 'Image', 'Post format' ),
'quote' => _x( 'Quote', 'Post format' ),
'status' => _x( 'Status', 'Post format' ),
'video' => _x( 'Video', 'Post format' ),
'audio' => _x( 'Audio', 'Post format' ),
);
return $strings;
}
*/
$formats = array('video');
add_theme_support( 'post-formats', $formats );
//得到当前Post type
//$format = get_post_format();
//调用
//$video_desc = get_post_meta(get_the_ID(), 'video_desc1', true);
function f_admin_scripts() {
wp_register_script('f-post-formats',plugin_dir_url( __FILE__ ).'/f_post_formats.js' , array('jquery'));
wp_enqueue_script('f-post-formats');
}
add_action('admin_print_scripts', 'f_admin_scripts');
?>
js的内容如下:
jQuery(function(){
jQuery('#post-formats-select input').change(mataChange);
function hidiAllPostMatas(){
jQuery('#f-meta-box-video').css('display','none');
}
function displayVideoFormatMata(){
hidiAllPostMatas();
jQuery('#f-meta-box-video').css('display','block');
}
function mataChange(){
hidiAllPostMatas();
if( jQuery('#post-format-video').attr('checked') ){
displayVideoFormatMata();
}
}
mataChange();
});