Hi tjsix,
I’ve taken your code above and put it in it’s own file and included that file in functions.php. It returned an error when I went to post.php saying there was an extra } at the end so I’ve removed it.
Here’s the code as it is now:
<?php
/**
* This file handles the custom meta boxes found on posts (and pages once I've worked them into it).
*/
// Put all your arguments and details in the array. Example:
$meta_boxes = array(
"background_image" => array(
"name" => "background_image",
"title" => "Page Background Image",
"type" => "text",
"description" => "A background image for the page."
)
);
// Hook into the admin_menu
add_action('admin_menu', 'create_meta_box');
function create_meta_box() {
add_meta_box( 'meta-box-id', 'The Title', 'display_meta_box', 'post', 'normal', 'high' );
}
// Display the meta_boxes
function display_meta_box($meta_boxes) {
$data = get_post_meta($post->ID, $key, true);
?><div class="form_wrap">
<?php foreach($meta_boxes as $meta_box) { ?>
<div class="form_field">
<label for="<?php echo $meta_box['name']; ?>"><?php echo $meta_box['title']; ?></label>
<input type="text" name="<?php echo $meta_box['name']; ?>" value="<?php echo htmlspecialchars( $data[$meta_box['name']] ); ?>" />
<p><?php echo $meta_box[ 'description' ]; ?></p>
</div>
<?php } ?>
</div>
<?php }
// Function to save your fields
add_action('save_post', 'save_meta_box');
function save_meta_box( $post_id ) {
global $post;
foreach( $meta_boxes as $meta_box ) {
$data[ $meta_box[ 'name' ] ] = $_POST[ $meta_box[ 'name' ] ];
}
//Make sure the POST info came from WP.
if ( !wp_verify_nonce( $_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__) ) ) return $post_id;
if ( !current_user_can( 'edit_post', $post_id )) return $post_id;
update_post_meta( $post_id, $key, $data );
}
?>
It’s done something very strange when the page is displayed, have a look here: https://www.rsimpson.net/strange-meta-behaviour.gif
Have you ever seen this before?
Cheers,
Rab