<?php
$new_meta_boxes =
array(
"area" => array(
"name" => "area",
"type" => "input",
"std" => "Unknown",
"title" => "Area",
"description" => "Where is the person located? (ex: Los Angeles, CA)"),
"age" => array(
"name" => "age",
"type" => "input",
"std" => "Unknown",
"title" => "Age",
"description" => "How old is this person"),
"country" => array(
"name" => "country",
"type" => "select",
"std" => "USA",
"title" => "Country",
"options" => array("USA","Europe","Asia","Australia"),
"description" => "Country of origin. This should be obvious"),
"attachments" => array(
"name" => "attachments",
"type" => "attachment",
"std" => "",
"title" => "Attachment",
"description" => "Choose an attachment")
);
$new_meta_boxes_2 =
array(
"image" => array(
"name" => "image",
"type" => "input",
"std" => "",
"title" => "Image",
"description" => "Paste image URL, ex: http://www.mydomain/folder/my_image_name.jpg"),
"imgalt" => array(
"name" => "imgalt",
"type" => "input",
"std" => "",
"title" => "Image Description",
"description" => "Enter a description for the image (alt tag).")
);
function new_meta_boxes() {
global $post, $new_meta_boxes, $new_meta_boxes_2;
foreach($new_meta_boxes as $meta_box) {
echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
echo'<h2>'.$meta_box['title'].'</h2>';
if( $meta_box['type'] == "input" ) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="55" /><br />';
} elseif ( $meta_box['type'] == "select" ) {
echo'<select name="'.$meta_box['name'].'_value">';
foreach ($meta_box['options'] as $option) {
echo'<option';
if ( get_post_meta($post->ID, $meta_box['name'].'_value', true) == $option ) {
echo ' selected="selected"';
} elseif ( $option == $meta_box['std'] ) {
echo ' selected="selected"';
}
echo'>'. $option .'</option>';
}
echo'</select>';
} elseif ( $meta_box['type'] == "attachment" ) {
echo'<select name="'.$meta_box['name'].'_value">';
$outer_post = $post->ID;
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
$option = $post->guid;
echo'<option value="';
if ( get_post_meta($outer_post, $meta_box['name'].'_value', true) == $option ) {
echo ' selected="selected"';
} elseif ( $option == $meta_box['std'] ) {
echo ' selected="selected"';
}
echo $option .'">'.$post->post_title.'</option>';
}
}
echo'</select>';
}
echo'<p><label for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label></p>';
}
}
function new_meta_boxes_2() {
global $post, $new_meta_boxes, $new_meta_boxes_2;
foreach($new_meta_boxes_2 as $meta_box) {
echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
echo'<h2>'.$meta_box['title'].'</h2>';
if( $meta_box['type'] == "input" ) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" size="55" /><br />';
} elseif ( $meta_box['type'] == "select" ) {
echo'<select name="'.$meta_box['name'].'_value">';
foreach ($meta_box['options'] as $option) {
echo'<option';
if ( get_post_meta($post->ID, $meta_box['name'].'_value', true) == $option ) {
echo ' selected="selected"';
} elseif ( $option == $meta_box['std'] ) {
echo ' selected="selected"';
}
echo'>'. $option .'</option>';
}
echo'</select>';
}
echo'<p><label for="'.$meta_box['name'].'_value">'.$meta_box['description'].'</label></p>';
}
}
function create_meta_box() {
global $theme_name, $new_meta_boxes, $new_meta_boxes_2;
if (function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'More Info', 'new_meta_boxes', 'movies', 'normal', 'high' );
add_meta_box( 'new-meta-boxes_2', 'Images', 'new_meta_boxes_2', 'page', 'normal', 'high' );
}
}
function save_postdata( $post_id ) {
global $post, $new_meta_boxes;
foreach($new_meta_boxes as $meta_box) {
if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;
}
$data = $_POST[$meta_box['name'].'_value'];
if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
update_post_meta($post_id, $meta_box['name'].'_value', $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
}
}
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
?>