Skip to content

Instantly share code, notes, and snippets.

@jhgreenblatt
Created May 21, 2010 13:49
Show Gist options
  • Select an option

  • Save jhgreenblatt/408852 to your computer and use it in GitHub Desktop.

Select an option

Save jhgreenblatt/408852 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Custom Write Panel
Plugin Adaptation: Simplified and commented by Jonathan Greenblatt
Coppied from: Gallery a Child Theme of Thematic by Chris Wallace
Chris Wallace URI: http://www.chris-wallace.com/
Gallery URI: http://themeshaper.com/gallery-theme-thematic/
Original Plugin URI: http://wefunction.com/2008/10/tutorial-create-custom-write-panels-in-wordpress
Description: Allows custom fields to be added to the WordPress Post Page
Version: 1.0
Author: Spencer
Author URI: http://wefunction.com
/* ----------------------------------------------*/
//begin by creating arrays as an easy way to store and call the information.
//this is the primary portion of the code that you will customize and duplicate.
//this example only covers a basic text input
$new_meta_boxes =
array(
"input-one" => array(
"name" => "input-one",
"title" => "example one of a basic text input",
"description" => "This is where you enter your description. It will display beneath the text input."),
"input-two" => array(
"name" => "input-two",
"title" => "example two of a basic text input",
"description" => "This is where you enter your description. It will display beneath the text input."),
);
//end of array
//now we begin a function to create the html that comprise the new meta boxes
//below you can edit/add inline css if you want to change how the label and description appear on the admin page
//worth testing, but not important to manipulate this portion of the code unless you have a super specific objective in mind.
function new_meta_boxes() {
global $post, $new_meta_boxes;
foreach($new_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'], true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename"
value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
echo'<label style="font-weight: bold; display: block; padding: 5px 0 2px 2px"
for="'.$meta_box['name'].'">'.$meta_box['title'].'</label>';
echo'<input type="text" name="'.$meta_box['name'].'" value="'.$meta_box_value.'" size="55" /><br />';
echo'<p><label for="'.$meta_box['name'].'">'.$meta_box['description'].'</label></p>';
}
}//end of new_meta_boxes() function
//another function begins here. This is what actually creates the meta box. The one you see on the admin page.
//you should edit the title for your meta box below. don't worry about anything else
function create_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'new-meta-boxes', 'Place the Title For Your Box Here', 'new_meta_boxes', 'post', 'normal', 'high' );
}
}//end of create_meta_box() function
//the third and most important function. this is what effectively saves your meta data into your database.
//DO NOT EDIT unless you have expert php skills or advice.
function save_postdata( $post_id ) {
global $post, $new_meta_boxes;
foreach($new_meta_boxes as $meta_box) {
// Verify
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']];
if(get_post_meta($post_id, $meta_box['name']) == "")
add_post_meta($post_id, $meta_box['name'], $data, true);
elseif($data != get_post_meta($post_id, $meta_box['name'], true))
update_post_meta($post_id, $meta_box['name'], $data);
elseif($data == "")
delete_post_meta($post_id, $meta_box['name'], get_post_meta($post_id, $meta_box['name'], true));
}
}//end of save_postdata () function
//these are action hooks that place your special functions defined above onto the admin page.
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment