Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Edit 12/15/2011 Added Radio Button print code

  // Add meta boxes to admin panel only needs to be added once
  add_action('admin_menu', 'plib_add_box');

  //Add meta boxes to post types
      function plib_add_box() {
          global $meta_box;

          foreach($meta_box as $post_type => $value) {
              add_meta_box($value['id'], $value['title'], 'plib_format_box', $post_type, $value['context'], $value['priority']);
          }
      }

  //Format meta boxes
      function plib_format_box() {
        global $meta_box, $post;

  // Use once for verification
  echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';

  echo '<table class="form-table">';

  foreach ($meta_box[$post->post_type]['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['default']) . '" size="30" style="width:30%" />'. ' - '. $field['desc'];
                    break;
                case 'textarea':
                    echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</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 '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 plib_save_data($post_id) {
          global $meta_box,  $post;

          //Verify nonce
          if (!wp_verify_nonce($_POST['plib_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[$post->post_type]['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);
              }
          }
      }

  add_action('save_post', 'plib_save_data');

End Edit on 12/15/2011

Edit #2 Showing Printr output

Array ( 
[_edit_last] => Array ( [0] => 1 ) [_edit_lock] => Array ( [0] => 1323951531:1 )     
[start_date] => Array ( [0] => 2011/11/18 ) [end_date] => Array ( [0] => 2011/12/31 ) 
[opening_time] => Array ( [0] => 2011-11-21 18:00 ) 
[artist_talk_time] => Array ( [0] => 2011-11-22 17:00 ) 
[_thumbnail_id] => Array ( [0] => 130 ) 
[_start_month] => Array ( [0] => 01 ) 
[_start_day] => Array ( [0] => 77 ) 
[_start_hour] => Array ( [0] => 03 ) 
[_start_year] => Array ( [0] => 2011 ) 
[_start_minute] => Array ( [0] => 00 ) 
[_start_eventtimestamp] => Array ( [0] => 201101770300 ) 
[_end_month] => Array ( [0] => 12 ) 
[_end_day] => Array ( [0] => 10 ) 
[_end_hour] => Array ( [0] => 03 ) 
[_end_year] => Array ( [0] => 2011 ) 
[_end_minute] => Array ( [0] => 00 ) 
[_end_eventtimestamp] => Array ( [0] => 201112100300 ) 
[event_venue] => Array ( [0] => Pine Gallery ) 
[closing_time] => Array ( [0] => 2011-12-22 19:00 ) 
[lecture_time] => Array ( [0] => 2011-11-21 19:00 ) 
[panel_time] => Array ( [0] => 2011-11-24 14:00 ) 
[special_event_time] => Array ( [0] => 2011-11-28 19:00 ) 
[workshop_time] => Array ( [0] => 2011-11-28 13:00 ) ) 

End Edit #2

From great advice on this site I changed my use of get_post_meta to get_post_custom to keep the number of queries down. Now I am having a trouble getting two values from an array set by a radio button choice.

The goal is to set the venue by clicking the radio button and have it set the url of the venue that is pre-set in the array. I want to be able to echo both values.

Here is the code that I am using.

       <?php
       /* creating meta boxes in functions.php */
       $meta_box['event'] = array(
          'id' => 'event-meta-details',
          'title' => 'Event Information',
          'context' => 'normal',
          'priority' => 'high',
          'fields' => array(                  
              array(
                  'name' => 'Workshop',
                  'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
                  'id' => 'workshop_time',
                  'type' => 'text',
                  'default' => ''
              ),
              array(
              'name' => 'Venue',
              'desc' => 'Venue of Event',
              'id' => $prefix . 'event_venue',
              'type' => 'radio',
              'options' => array(   
              array('name' => 'Pine Gallery', 'value' => 'Pine Gallery', 'http://www.pinegallery.com' ),
              array('name' => 'Spruce Gallery', 'value' => 'Spruce  Gallery'),
              array('name' => 'Oak Gallery', 'value' => 'Oak Gallery')
              )
              ),
              array(
                  'name' => 'Custom Venue',
                  'desc' => 'Enter Venue Name if Enter Text Below is Checked',
                  'id' => 'custom_event_venue',
                  'type' => 'text',
                  'default' => ''
              ),
              array(
                  'name' => 'Featured / Reccomended',
                  'desc' => 'Recommended Event',
                  'id' => 'featured_event',
                  'type' => 'checkbox',
                  'default' => ''
                )

          )
      );

      /* In the post trying to get both keys in the array*/

      $event_custom_meta=get_post_custom($post->ID); // Get all the data 

      $event_event_venue = $event_custom_meta['event_venue'][0];
      $event_url = $event_custom_meta['event_venue'][1];

      echo $event_event_venue;
      echo $event_url;
      ?>

Thank you.

share|improve this question
    
can you add the code that prints out the radio button –  Bainternet Dec 15 '11 at 9:27
    
@Bainternet I added the radio button code above. Thank you for the help. –  John Bentwin Dec 15 '11 at 12:16
add comment

2 Answers

up vote 1 down vote accepted

The main problem with your code is that you only store the value and not the venue name a simple fix would be to change your vlaue to hold the venue name as well , something like:

array(
    'name' => 'Venue',
    'desc' => 'Venue of Event',
    'id' => $prefix . 'event_venue',
    'type' => 'radio',
    'options' => array(   
        array('name' => 'Pine Gallery', 'value' => 'Pine Gallery***http://www.pinegallery.com' ),
        array('name' => 'Spruce Gallery', 'value' => 'Spruce  Gallery***http://www.sprucegallery.com'),
        array('name' => 'Oak Gallery', 'value' => 'Oak Gallery***http://www.oakgallery.com')
    )
)

and the other problem with your code is that you are looking for $event_custom_meta['event_venue'] and there is no field named event_venue since you define it using a $prifix so it should be $event_custom_meta[$prifix.'event_venue']

so now $event_custom_meta[$prifix.'event_venue'][0] should have but name and URL separated by *** so split it to an array and you will have bit values, something like this:

$event_venue = $pieces = explode("***", $event_custom_meta[$prifix.'event_venue'][0]);
echo 'name: '.$event_venue[0];
echo '<br/>url: '.$event_venue[1];

Hope this helps

share|improve this answer
    
Thank you for the help. That code works but what if sometimes I wanted to only echo the url and not the venue name? Coule I set it up like this? array('name' => 'Pine Gallery', 'value' => array('Pine Gallery', 'http://www.pinegallery.com') ), or array('name' => 'Pine Gallery', array('value' => 'Pine Gallery','value' => 'http://www.pinegallery.com') ), or array('name' => 'Pine Gallery', array('value' => 'Pine Gallery','url' => 'http://www.pinegallery.com') ), –  John Bentwin Dec 15 '11 at 12:43
1  
Storing the value as an array is not possible with the code you are using to generate the metabox, so setting the 'value' => array(... is no good in your case but if you don't want to show the name or the url just add the value you want and echo that out, and in the cases you want both, use the snippet above to split it to and array and print both. –  Bainternet Dec 15 '11 at 12:50
    
Ok So I will use the explode code to echo them both but I am confused when you say "if you don't want to sho the name or the url just add the value you want and echo that out," How do I add the value "I want" to just echo the url and not the name? I want to be able to have them both written out in functions.php so I don't have to re-type them for each event. It seems no matter what I do it only echos out the first value. Am I misunderstanding? –  John Bentwin Dec 15 '11 at 13:01
1  
yes you are,to make it simple, add the value using *** as a delimiter like i did and when you want to show the name echo $array[0], and to show url echo $array[1] and to show both echo both. –  Bainternet Dec 15 '11 at 13:56
    
Thank you, I understand now. –  John Bentwin Dec 15 '11 at 14:53
add comment

It looks like you are calling the wrong fields.

try print_r($event_custom_meta); to see the correct ones.

share|improve this answer
    
Thank you for the suggestion. I tried print-r($event_custom_meta); and it printed all the fields in the the array but did not print the url. I want to be able to click the radio button once and have the venue and the venue url set and then be able to echo them. I can echo the venue name - In this example "Pine Gallery," but I cannot echo the url "pinegallery.com."; –  John Bentwin Dec 15 '11 at 12:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.