Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- global $my_options;
- /* ********************************************** */
- /* Add Post Meta Boxes */
- /* adapted from: http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html
- /* ********************************************** */
- /*********************************
- Gets Cform form names from cforms meta data
- **********************************/
- $cform_settings = get_option('cforms_settings'); //call cforms meta data
- $num_items = count($cform_settings); // counts the number of items (arrays) in the array
- $num_forms = $num_items -1; //since first array is [global] it can be subtracted to determin # of saved forms
- $cforms = array(); //my array with only the form ID# and names
- $i=1;
- while ($i<= $num_forms) { //while loop to populate my array
- $key1 = "form".$i;
- $key2 = "cforms".$i."_fname"; //forms are defined by meta data $cforms[form#][cforms#_fname]
- if ($i==1) {
- $key1 = "form"; //first form is defined by meta data $cforms[form][cforms_fname]
- $key2 = "cforms_fname";
- }
- $form_name = $cform_settings[$key1][$key2] ; //pulls name of form from meta data
- $add = array('desc'=>$form_name,'value'=>$i); //create subarray of name and value info
- $cforms[] = $add; //adds subarray to main $cforms array
- $i++;
- }
- /******************************
- Edit meta box settings here
- ******************************/
- $prefix = '_dbt_';
- $meta_boxes = array();
- // first meta box
- $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
- $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
- if ($template_file == 'landingpage.php') {
- $meta_boxes[] = array(
- 'id' => 'my-meta-box-1',
- 'title' => 'Landing Page Elements',
- 'pages' => array('post', 'page', 'link'), // multiple post types
- 'context' => 'side',
- 'priority' => 'low',
- 'fields' => array(
- array(
- 'name' => 'Contact Form',
- 'desc' => 'Enter the CForm ID number to add a CForm to the landing page',
- 'id' => $prefix . 'cform_id',
- 'type' => 'select2',
- 'options' => $cforms,
- 'std' => ''
- ),
- array(
- 'name' => 'Headline',
- 'desc' => 'Enter the heading you\'d like to appear before the video',
- 'id' => $prefix . 'heading1',
- 'type' => 'text',
- 'std' => ''
- ),
- /* array(
- 'name' => 'Textarea',
- 'desc' => 'Enter big text here',
- 'id' => $prefix . 'textarea',
- 'type' => 'textarea',
- 'std' => 'Default value 2'
- ) */
- )
- );
- } //end if statement
- // second meta box
- /*
- $meta_boxes[] = array(
- 'id' => 'my-meta-box-2',
- 'title' => 'Custom meta box 2',
- 'pages' => array('post', 'link', 'film', 'album'), // custom post type
- 'context' => 'normal',
- 'priority' => 'high',
- 'fields' => array(
- array(
- 'name' => 'Select box',
- 'id' => $prefix . 'select',
- 'type' => 'select',
- 'options' => array('Option 1', 'Option 2', 'Option 3')
- ),
- array(
- 'name' => 'Radio',
- 'id' => $prefix . 'radio',
- 'type' => 'radio',
- 'options' => array(
- array('name' => 'Name 1', 'value' => 'Value 1'),
- array('name' => 'Name 2', 'value' => 'Value 2')
- )
- ),
- array(
- 'name' => 'Checkbox',
- 'id' => $prefix . 'checkbox',
- 'type' => 'checkbox'
- )
- )
- );
- */
- /*********************************
- You should not edit the code below
- *********************************/
- foreach ($meta_boxes as $meta_box) {
- $my_box = new My_meta_box($meta_box);
- }
- class My_meta_box {
- protected $_meta_box;
- // create meta box based on given data
- function __construct($meta_box) {
- $this->_meta_box = $meta_box;
- add_action('admin_menu', array(&$this, 'add'));
- add_action('save_post', array(&$this, 'save'));
- }
- /// Add meta box for multiple post types
- function add() {
- foreach ($this->_meta_box['pages'] as $page) {
- add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
- }
- }
- // Callback function to show fields in meta box
- function show() {
- global $post;
- // Use nonce for verification
- echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
- echo '<table class="form-table">';
- foreach ($this->_meta_box['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'], '">', $field['name'], '</label></th>',
- '<td>';
- switch ($field['type']) {
- case 'text':
- echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
- '<br />', $field['desc'];
- break;
- case 'textarea':
- echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
- '<br />', $field['desc'];
- break;
- case 'select':
- 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 'select2': //for when value and display text don't match
- //$options array must be multidimensional with both the 'value' and the 'text' for each option
- //for example = array( array(text=>text1,value=>value1),array(text=>text2,value=>value2))
- //then in <option> value = $option['value'] and text = $option['text']
- echo '<select name="', $field['id'], '" id="', $field['id'], '">';
- foreach ($field['options'] as $option) {
- echo '<option value="',$option['value'],'"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['desc'], '</option>';
- }
- echo '</select>';
- break;
- case 'radio':
- 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':
- echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
- break;
- }
- echo '<td>',
- '</tr>';
- }
- echo '</table>';
- }
- // Save data from meta box
- function save($post_id) {
- // verify nonce
- if (!wp_verify_nonce($_POST['mytheme_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 ($this->_meta_box['fields'] as $field) {
- $old = get_post_meta($post_id, $field['id'], true);
- $new = $_POST[$field['id']];
- if ($new && $new != $old) {
- update_post_meta($post_id, $field['id'], $new);
- } elseif ('' == $new && $old) {
- delete_post_meta($post_id, $field['id'], $old);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement