• hey gang. I’ve been staring at this for hours trying to figure out why this custom meta box’s value isn’t saving but can’t see anything wrong. Here’s the complete code in my function:

    Any help’s GREATLY appreciated!

    <?php
    // custom meta box for pages to select a custom menu from the ones avaiable
    
    // get all the custom menus
    function mymenu_get_all_menus() {
      $menu_obj = get_terms( 'nav_menu' );
      return $menu_obj;
    }
    
    function mymenu_generate_menu_from_items($items) {
      if(count($items)>0):
        $menu_list = '<ul id="menu-' . $menu_to_use . '">';
          foreach ( (array) $items as $key => $menu_item ) {
            $title = $menu_item->title;
            $url = $menu_item->url;
            $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
          }
        $menu_list .= '</ul>';
        return $menu_list;
      endif;
    }
    
    // add the meta box
    $meta_box_menu = array(
        'id' => 'mymenu-meta-menu',
        'title' => 'Custom Menu',
        'page' => 'page',
        'context' => 'side',
        'priority' => 'low',
        'fields' => array(
            array(
                'id' => 'mymenu-meta-menu-name',
                'type' => 'select',
                'std' => 'none'
            ),
        ),
    );
    
    // This function will register the meta box with WordPress
    function mymenu_add_box() {
        global $meta_box_menu;
        add_meta_box($meta_box_menu['id'], $meta_box_menu['title'], 'mymenu_meta_menu_html', $meta_box_menu['page'], $meta_box_menu['context'], $meta_box_menu['priority']);
    }
    add_action('admin_init', 'mymenu_add_box');
    
    // This function will produce the html needed to display our meta box in the admin area
    
    function mymenu_meta_menu_html($post_id) {
      global $meta_box_menu, $post;
    
      $output = '<p style="padding:10px 0 0 0;">'.__('Choose which custom menu shows up on this page. This will override any selections made in Appearance > Menus', 'mymenu').'</p>';
      $output .= '<input type="hidden" name="rt_meta_box_nonce" value="'. wp_create_nonce(basename(__FILE__)). '" />';
    
      $output .= '<table class="form-table">';
    
      foreach ($meta_box_menu['fields'] as $field) {
        $meta = get_post_meta($post->ID, $field['id'], true);
    
        //  Get out all our menus using the function from functions.php
    
        $menus = mymenu_get_all_menus();
    
        //  Grab out saved data for edit mode
    
        $meta = get_post_meta($post->ID, $field['id'], true);
    
        $output .= '<select name="'.$field['id'].'" class="widefat">';
        $output .= '<option value="none">- none -</option>';
        if(is_array($menus)):
          foreach($menus as $k => $v):
            if($meta==$v->slug):
              $output .= '<option selected="selected" value="' . $v->slug .'">' . $v->name . '</option>';
            else:
              $output .= '<option value="' . $v->slug .'">' . $v->name . '</option>';
            endif;
          endforeach;
        endif;
        $output .= '</select>';
    
      }
    
      $output .= '</table>';
    
      echo $output;
    }
    
    // This function will save our preferences into the database
    
    function mymenu_save_data($post_id) {
     global $meta_box, $meta_box_menu;
    
    	if (!wp_verify_nonce($_POST['rt_meta_box_nonce'], basename(__FILE__))) {
    		return $post_id;
    	}
    
    	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    		return $post_id;
    	}
    
    	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_menu['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'], stripslashes(htmlspecialchars($new)));
    		} elseif ('' == $new && $old) {
    			delete_post_meta($post_id, $field['id'], $old);
    		}
    	}
    }
    ?>

The topic ‘Custom Meta Box not saving’ is closed to new replies.