Save meta box data from selected dropdown list

freaky picture freaky · Jul 19, 2013 · Viewed 17.1k times · Source

I'm trying to save some data in WordPress database from a meta box.

I've got a dropdown list to select some options and I want to save the selected option in database thanks to meta box.

However I have some difficulty with the save function in PHP :

<?php

function add_admin_menu_class_meta_box() {
    $pages = array('post', 'portfolio');
    foreach( $pages as $page ) {
        add_meta_box('custom_element_grid_class','Element grid size', 'custom_element_grid_class_meta_box', $page, 'side', 'high');
    }
}
add_action( 'admin_menu', 'add_admin_menu_class_meta_box' );

function custom_element_grid_class_meta_box(){

    ?>

    <label>Choose the size of the element :  </label>

    <select name="custom_element_grid_class" id="custom_element_grid_class">
      <option value="normal" <?php selected( $meta_element_class, 'normal' ); ?>>normal</option>
      <option value="square" <?php selected( $meta_element_class, 'square' ); ?>>square</option>
      <option value="wide" <?php selected( $meta_element_class, 'wide' ); ?>>wide</option>
      <option value="tall" <?php selected( $meta_element_class, 'tall' ); ?>>tall</option>
    </select>


    <?php
}

add_action('save_post', 'save_custom_element_grid_class');
function save_custom_element_grid_class(){

global $post;

if(!isset($_POST["custom_element_grid_class"])):
    return $post;
    endif;
    update_post_meta($post->ID, 'custom_element_grid_class', $meta_element_class);
}

?>

How can I get the select value an save it with update_post_meta()?

Answer

Jean Paul picture Jean Paul · Jul 20, 2013

Here is the code I tested and it works:

<?php
/**
* Plugin Name: Metabox test
*
*/

add_action( 'add_meta_boxes', 'so_custom_meta_box' );

function so_custom_meta_box($post){
    add_meta_box('so_meta_box', 'Custom Box', 'custom_element_grid_class_meta_box', $post->post_type, 'normal' , 'high');
}

add_action('save_post', 'so_save_metabox');

function so_save_metabox(){ 
    global $post;
    if(isset($_POST["custom_element_grid_class"])){
         //UPDATE: 
        $meta_element_class = $_POST['custom_element_grid_class'];
        //END OF UPDATE

        update_post_meta($post->ID, 'custom_element_grid_class_meta_box', $meta_element_class);
        //print_r($_POST);
    }
}

function custom_element_grid_class_meta_box($post){
    $meta_element_class = get_post_meta($post->ID, 'custom_element_grid_class_meta_box', true); //true ensures you get just one value instead of an array
    ?>   
    <label>Choose the size of the element :  </label>

    <select name="custom_element_grid_class" id="custom_element_grid_class">
      <option value="normal" <?php selected( $meta_element_class, 'normal' ); ?>>normal</option>
      <option value="square" <?php selected( $meta_element_class, 'square' ); ?>>square</option>
      <option value="wide" <?php selected( $meta_element_class, 'wide' ); ?>>wide</option>
      <option value="tall" <?php selected( $meta_element_class, 'tall' ); ?>>tall</option>
    </select>
    <?php
}