Advertisement
Guest User

WordPress MetaBoxes Adaptation

a guest
Aug 24th, 2010
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.13 KB | None | 0 0
  1. <?php
  2.  
  3. global $my_options;
  4.  
  5. /* ********************************************** */
  6. /* Add Post Meta Boxes  */
  7. /* adapted from: http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html
  8. /* ********************************************** */
  9.  
  10. /*********************************
  11. Gets Cform form names from cforms meta data
  12. **********************************/
  13.  
  14. $cform_settings = get_option('cforms_settings'); //call cforms meta data
  15. $num_items = count($cform_settings); // counts the number of items (arrays) in the array
  16. $num_forms = $num_items -1; //since first array is [global] it can be subtracted to determin # of saved forms
  17. $cforms = array(); //my array with only the form ID# and names
  18. $i=1;
  19. while ($i<= $num_forms) { //while loop to populate my array
  20. $key1 = "form".$i;
  21. $key2 = "cforms".$i."_fname"; //forms are defined by meta data $cforms[form#][cforms#_fname]
  22. if ($i==1) {
  23.     $key1 = "form";  //first form is defined by meta data $cforms[form][cforms_fname]
  24.     $key2 = "cforms_fname";
  25.     }
  26.  
  27. $form_name = $cform_settings[$key1][$key2] ; //pulls name of form from meta data
  28.    
  29. $add = array('desc'=>$form_name,'value'=>$i);  //create subarray of name and value info
  30. $cforms[] = $add;   //adds subarray to main $cforms array
  31.     $i++;
  32.     }
  33.    
  34. /******************************
  35.  
  36. Edit meta box settings here
  37.  
  38. ******************************/
  39.  
  40. $prefix = '_dbt_';
  41.  
  42. $meta_boxes = array();
  43.  
  44. // first meta box
  45.  
  46.  $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
  47.  $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
  48.  
  49.  if ($template_file == 'landingpage.php') {
  50.  
  51. $meta_boxes[] = array(
  52.     'id' => 'my-meta-box-1',
  53.     'title' => 'Landing Page Elements',
  54.     'pages' => array('post', 'page', 'link'), // multiple post types
  55.     'context' => 'side',
  56.     'priority' => 'low',
  57.     'fields' => array(
  58.         array(
  59.             'name' => 'Contact Form',
  60.             'desc' => 'Enter the CForm ID number to add a CForm to the landing page',
  61.             'id' => $prefix . 'cform_id',
  62.             'type' => 'select2',
  63.             'options' => $cforms,
  64.             'std' => ''
  65.         ),
  66.         array(
  67.             'name' => 'Headline',
  68.             'desc' => 'Enter the heading you\'d like to appear before the video',
  69.             'id' => $prefix . 'heading1',
  70.             'type' => 'text',
  71.             'std' => ''
  72.         ),
  73.         /* array(
  74.             'name' => 'Textarea',
  75.             'desc' => 'Enter big text here',
  76.             'id' => $prefix . 'textarea',
  77.             'type' => 'textarea',
  78.             'std' => 'Default value 2'
  79.         ) */
  80.     )
  81. );
  82.  
  83. } //end if statement
  84.  
  85. // second meta box
  86. /*
  87. $meta_boxes[] = array(
  88.     'id' => 'my-meta-box-2',
  89.     'title' => 'Custom meta box 2',
  90.     'pages' => array('post', 'link', 'film', 'album'), // custom post type
  91.     'context' => 'normal',
  92.     'priority' => 'high',
  93.     'fields' => array(
  94.         array(
  95.             'name' => 'Select box',
  96.             'id' => $prefix . 'select',
  97.             'type' => 'select',
  98.             'options' => array('Option 1', 'Option 2', 'Option 3')
  99.         ),
  100.         array(
  101.             'name' => 'Radio',
  102.             'id' => $prefix . 'radio',
  103.             'type' => 'radio',
  104.             'options' => array(
  105.                 array('name' => 'Name 1', 'value' => 'Value 1'),
  106.                 array('name' => 'Name 2', 'value' => 'Value 2')
  107.             )
  108.         ),
  109.         array(
  110.             'name' => 'Checkbox',
  111.             'id' => $prefix . 'checkbox',
  112.             'type' => 'checkbox'
  113.         )
  114.     )
  115. );
  116. */
  117.  
  118.    
  119. /*********************************
  120.  
  121. You should not edit the code below
  122.  
  123. *********************************/
  124.  
  125. foreach ($meta_boxes as $meta_box) {
  126.     $my_box = new My_meta_box($meta_box);
  127. }
  128.  
  129. class My_meta_box {
  130.  
  131.     protected $_meta_box;
  132.  
  133.     // create meta box based on given data
  134.     function __construct($meta_box) {
  135.         $this->_meta_box = $meta_box;
  136.         add_action('admin_menu', array(&$this, 'add'));
  137.  
  138.         add_action('save_post', array(&$this, 'save'));
  139.     }
  140.  
  141.     /// Add meta box for multiple post types
  142.     function add() {
  143.         foreach ($this->_meta_box['pages'] as $page) {
  144.             add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
  145.         }
  146.     }
  147.  
  148.     // Callback function to show fields in meta box
  149.     function show() {
  150.         global $post;
  151.  
  152.         // Use nonce for verification
  153.         echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  154.    
  155.         echo '<table class="form-table">';
  156.  
  157.         foreach ($this->_meta_box['fields'] as $field) {
  158.             // get current post meta data
  159.             $meta = get_post_meta($post->ID, $field['id'], true);
  160.        
  161.             echo '<tr>',
  162.                     '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  163.                     '<td>';
  164.             switch ($field['type']) {
  165.                 case 'text':
  166.                     echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
  167.                         '<br />', $field['desc'];
  168.                     break;
  169.                 case 'textarea':
  170.                     echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
  171.                         '<br />', $field['desc'];
  172.                     break;
  173.                 case 'select':
  174.                     echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  175.                     foreach ($field['options'] as $option) {
  176.                         echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  177.                     }
  178.                     echo '</select>';
  179.                 break;
  180.                 case 'select2': //for when value and display text don't match
  181.                                 //$options array must be multidimensional with both the 'value' and the 'text' for each option
  182.                                 //for example = array( array(text=>text1,value=>value1),array(text=>text2,value=>value2))
  183.                                 //then in <option> value = $option['value'] and text = $option['text']
  184.                     echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  185.                     foreach ($field['options'] as $option) {
  186.                         echo '<option value="',$option['value'],'"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['desc'], '</option>';
  187.                     }
  188.                     echo '</select>';
  189.                     break;
  190.                 case 'radio':
  191.                     foreach ($field['options'] as $option) {
  192.                         echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  193.                     }
  194.                     break;
  195.                 case 'checkbox':
  196.                     echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  197.                     break;
  198.             }
  199.             echo    '<td>',
  200.                 '</tr>';
  201.         }
  202.    
  203.         echo '</table>';
  204.     }
  205.  
  206.     // Save data from meta box
  207.     function save($post_id) {
  208.         // verify nonce
  209.         if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
  210.             return $post_id;
  211.         }
  212.  
  213.         // check autosave
  214.         if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  215.             return $post_id;
  216.         }
  217.  
  218.         // check permissions
  219.         if ('page' == $_POST['post_type']) {
  220.             if (!current_user_can('edit_page', $post_id)) {
  221.                 return $post_id;
  222.             }
  223.         } elseif (!current_user_can('edit_post', $post_id)) {
  224.             return $post_id;
  225.         }
  226.  
  227.         foreach ($this->_meta_box['fields'] as $field) {
  228.             $old = get_post_meta($post_id, $field['id'], true);
  229.             $new = $_POST[$field['id']];
  230.    
  231.             if ($new && $new != $old) {
  232.                 update_post_meta($post_id, $field['id'], $new);
  233.             } elseif ('' == $new && $old) {
  234.                 delete_post_meta($post_id, $field['id'], $old);
  235.             }
  236.         }
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement