Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // META BOX CODE
- $my_info_metabox = array(
- 'id' => 'my-info',
- 'title' => 'My Info',
- 'page' => array('my_info'),
- 'context' => 'normal',
- 'priority' => 'default',
- 'fields' => array(
- array(
- 'name' => 'Box 1',
- 'desc' => '',
- 'id' => 'myinfo-box1',
- 'class' => 'myinfo-box1',
- 'type' => 'textarea',
- 'rich_editor' => 1,
- 'max' => 0
- ),
- array(
- 'name' => 'Box 2',
- 'desc' => '',
- 'id' => 'myinfo-box2',
- 'class' => 'myinfo-box2',
- 'type' => 'textarea',
- 'rich_editor' => 1,
- 'max' => 0
- ),
- array(
- 'name' => 'Box 3',
- 'desc' => '',
- 'id' => 'myinfo-box3',
- 'class' => 'myinfo-box3',
- 'type' => 'textarea',
- 'rich_editor' => 1,
- 'max' => 0
- ),
- )
- );
- add_action('admin_init', 'add_my_info_meta_box');
- function add_my_info_meta_box() {
- global $my_info_metabox;
- foreach($my_info_metabox['page'] as $page) {
- add_meta_box($my_info_metabox['id'], $my_info_metabox['title'], 'show_my_info_box', $page, 'normal', 'default', $my_info_metabox);
- }
- }
- // function to show meta boxes
- function show_my_info_box() {
- global $post;
- global $my_info_metabox;
- global $wp_version;
- // Use nonce for verification
- echo '<input type="hidden" name="my_info_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
- echo '<table class="form-table">';
- foreach ($my_info_metabox['fields'] as $field) {
- // get current post meta data
- $meta = get_post_meta($post->ID, $field['id'], true);
- echo '<tr>',
- '<th style="width:20%"><label for="', $field['id'], '">', stripslashes($field['name']), '</label></th>',
- '<td class="myinfo_field_type_' . str_replace(' ', '_', $field['type']) . '">';
- switch ($field['type']) {
- case 'textarea':
- if($field['rich_editor'] == 1) {
- if($wp_version >= 3.3) {
- echo wp_editor($meta, $field['id'], array('textarea_name' => $field['id']));
- } else {
- // older versions of WP
- $editor = '';
- if(!post_type_supports($post->post_type, 'editor')) {
- $editor = wp_tiny_mce(true, array('editor_selector' => $field['class'], 'remove_linebreaks' => false) );
- }
- $field_html = '<div style="width: 97%; border: 1px solid #DFDFDF;"><textarea name="' . $field['id'] . '" class="' . $field['class'] . '" id="' . $field['id'] . '" cols="60" rows="8" style="width:100%">'. $meta . '</textarea></div><br/>' . __(stripslashes($field['desc']));
- echo $editor . $field_html;
- }
- } else {
- echo '<div style="width: 100%;"><textarea name="', $field['id'], '" class="', $field['class'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea></div>', '', stripslashes($field['desc']);
- }
- break;
- }
- echo '<td>',
- '</tr>';
- }
- echo '</table>';
- }
- // Save data from meta box
- add_action('save_post', 'my_info_metabox_save');
- function my_info_metabox_save($post_id) {
- global $post;
- global $my_info_metabox;
- // verify nonce
- if ( ! isset( $_POST['my_info_meta_box_nonce'] ) || ! wp_verify_nonce($_POST['my_info_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 ($my_info_metabox['fields'] as $field) {
- $old = get_post_meta($post_id, $field['id'], true);
- $new = $_POST[$field['id']];
- if ($new && $new != $old) {
- if($field['type'] == 'date') {
- $new = ssv_locations_format_date($new);
- update_post_meta($post_id, $field['id'], $new);
- } else {
- if(is_string($new)) {
- $new = $new;
- }
- update_post_meta($post_id, $field['id'], $new);
- }
- } elseif ('' == $new && $old) {
- delete_post_meta($post_id, $field['id'], $old);
- }
- }
- }
- // converts a time stamp to date string for meta fields
- if(!function_exists('my_info_timestamp_to_date')) {
- function my_info_timestamp_to_date($date) {
- return date('m/d/Y', $date);
- }
- }
- // was ecpt_format_date
- if(!function_exists('my_info_format_date')) {
- function my_info_format_date($date) {
- $date = strtotime($date);
- return $date;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement