Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This file handles the custom meta boxes found on posts and pages.
- */
- // post fields
- $post_fields = array(
- array(
- 'name' => 'Sub Title',
- 'desc' => 'Give this post a sub title?',
- 'id' => 'subtitle',
- 'type' => 'text',
- 'std' => ''
- ),
- array(
- 'name' => 'Additional Details',
- 'desc' => 'Enter extra post details here',
- 'id' => 'add_details',
- 'type' => 'textarea',
- 'std' => ''
- ),
- array(
- 'name' => 'Choose a Fruit',
- 'id' => 'fruit',
- 'type' => 'select',
- 'options' => array('Apples', 'Oranges', 'Pears', 'Pineapples')
- ),
- array(
- 'name' => 'Check 1',
- 'id' => 'check1',
- 'type' => 'checkbox',
- ),
- array(
- 'name' => 'Check 2',
- 'id' => 'check2',
- 'type' => 'checkbox',
- ),
- array(
- 'name' => 'Check 3',
- 'id' => 'check3',
- 'type' => 'checkbox',
- ),
- );
- // page fields
- $page_fields = array(
- array(
- 'name' => 'Sub Title',
- 'desc' => 'Give this post a sub title?',
- 'id' => 'subtitle',
- 'type' => 'text',
- 'std' => ''
- ),
- /* array(
- 'name' => 'Additional Details',
- 'desc' => 'Enter extra post details here',
- 'id' => 'add_details',
- 'type' => 'textarea',
- 'std' => ''
- ),
- array(
- 'name' => 'Choose a Fruit',
- 'id' => 'fruit',
- 'type' => 'select',
- 'options' => array('Apples', 'Oranges', 'Pears', 'Pineapples')
- ),
- array(
- 'name' => 'Check 1',
- 'id' => 'check1',
- 'type' => 'checkbox',
- ),
- array(
- 'name' => 'Check 2',
- 'id' => 'check2',
- 'type' => 'checkbox',
- ),
- array(
- 'name' => 'Check 3',
- 'id' => 'check3',
- 'type' => 'checkbox',
- ),
- */
- );
- // Add meta box to editor
- function rsgwd_meta_add_box() {
- global $post, $meta_box, $post_fields, $page_fields;
- $post_type = $post->post_type;
- switch($post_type) {
- case "post":
- $meta_box = array(
- 'id' => 'post_meta_box', // the id of our meta box
- 'title' => 'Post Options', // the title of the meta box
- 'page' => $post_type, // display this meta box on post editing screens
- 'context' => 'normal',
- 'priority' => 'high', // keep it near the top
- 'fields' => $post_fields
- );
- add_meta_box($meta_box['id'], $meta_box['title'], 'display_html', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
- break;
- case "page":
- $meta_box = array(
- 'id' => 'page_meta_box', // the id of our meta box
- 'title' => 'Page Options', // the title of the meta box
- 'page' => $post_type, // display this meta box on post editing screens
- 'context' => 'normal',
- 'priority' => 'high', // keep it near the top
- 'fields' => $page_fields
- );
- add_meta_box($meta_box['id'], $meta_box['title'], 'display_html', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
- break;
- default:
- break;
- }
- }
- add_action('add_meta_boxes', 'rsgwd_meta_add_box');
- // Callback function to show fields in meta box
- function display_html() {
- global $meta_box, $post; // get the variables from global $meta_box and $post
- // Use nonce for verification to check that the person has adequate priveleges
- echo '<input type="hidden" name="rsgwd_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
- // create the table which the options will be displayed in
- echo '<table class="form-table">';
- foreach ($meta_box['fields'] as $field) { // do this for each array inside of the fields array
- // get current post meta data
- $meta = get_post_meta($post->ID, $field['id'], true);
- echo '<tr>','<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>','<td>';
- switch ($field['type']) {
- case 'text': // the HTML to display for type=text options
- echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '', $field['desc'];
- break;
- case 'textarea': // the HTML to display for type=textarea options
- echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '', $field['desc'];
- break;
- case 'select': // the HTML to display for type=select options
- echo '<select name="', $field['id'], '" id="', $field['id'], '">';
- foreach ($field['options'] as $option) {echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';}
- echo '</select>';
- break;
- case 'radio': // the HTML to display for type=radio options
- foreach ($field['options'] as $option) {echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];}
- break;
- case 'checkbox': // the HTML to display for type=checkbox options
- echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
- break;
- }
- echo '<td>','</tr>';
- }
- echo '</table>';
- // echo "<pre>"; var_dump($meta_box['fields']); echo "</pre>";
- }
- // Save data from meta box
- function rsgwd_meta_save_data($post_id) {
- global $meta_box;
- // echo "<pre>"; var_dump($meta_box); echo "</pre>";
- // verify nonce -- checks that the user has access
- if (!wp_verify_nonce($_POST['rsgwd_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['fields'] as $field) { // save each option
- $old = get_post_meta($post_id, $field['id'], true);
- $new = $_POST[$field['id']];
- if ($new && $new != $old) { // compare changes to existing values
- update_post_meta($post_id, $field['id'], $new);
- } elseif ('' == $new && $old) {
- delete_post_meta($post_id, $field['id'], $old);
- }
- }
- }
- add_action('save_post', 'rsgwd_meta_save_data'); // save the data
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement