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.

I'm using a metabox script that works great for having absolute control over the design and placement of the metabox fields. There is one problem: I can't remember where I found the script and need to add a select box. This script does not use arrays as most metabox scripts do.

What function can I use to sucessfully add a select box?

// Add the Inventory Meta Boxes

function add_inventory_metaboxes() {
    add_meta_box('inventory_information', 'Inventory Information', 'inventory_information', 'inventory', 'side', 'default');
}

// The Event Location Metabox

function inventory_information() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="inventorymeta_noncename" id="inventorymeta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the location data if its already been entered
    $stocknum = get_post_meta($post->ID, '_dappcf_i_stocknum', true);
    $vin = get_post_meta($post->ID, '_dappcf_i_vin', true);
    $saleprice = get_post_meta($post->ID, '_dappcf_i_priceone', true);
    $internetprice = get_post_meta($post->ID, '_dappcf_i_pricetwo', true);
    $milage = get_post_meta($post->ID, '_dappcf_i_mileage', true);
    $carfaxurl = get_post_meta($post->ID, '_dappcf_i_carfaxurl', true);

    // Echo out the fields
    echo '<p>Stock #: <input type="text" name="_dappcf_i_stocknum" value="' . $stocknum  . '" class="widefat" style="width:80px" /> &nbsp;&nbsp;&nbsp; 
        Milage: <input type="text" name="_dappcf_i_mileage" value="' . $milage  . '" class="widefat"  style="width:80px" /> &nbsp;&nbsp;&nbsp; 
        VIN: <input type="text" name="_dappcf_i_vin" value="' . $vin  . '" class="widefat"  style="width:200px" />';
    echo '<p>Sale Price: <input type="text" name="_dappcf_i_priceone" value="' . $saleprice  . '" class="widefat"  style="width:80px" /> &nbsp;&nbsp;&nbsp; 
        Internet Price: <input type="text" name="_dappcf_i_pricetwo" value="' . $internetprice  . '" class="widefat"  style="width:80px" />';
    echo '<p>CarFax url: <input type="text" name="_dappcf_i_carfaxurl" value="' . $carfaxurl  . '" class="widefat"  style="width:170px" />';
}

// Save the Metabox Data

function txpbs_save_events_meta($post_id, $post) {

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['inventorymeta_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;

    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.

    $station_meta['_dappcf_i_stocknum'] = $_POST['_dappcf_i_stocknum'];
    $station_meta['_dappcf_i_vin'] = $_POST['_dappcf_i_vin'];
    $station_meta['_dappcf_i_priceone'] = $_POST['_dappcf_i_priceone'];
    $station_meta['_dappcf_i_pricetwo'] = $_POST['_dappcf_i_pricetwo'];
    $station_meta['_dappcf_i_mileage'] = $_POST['_dappcf_i_mileage'];
    $station_meta['_dappcf_i_carfaxurl'] = $_POST['_dappcf_i_carfaxurl'];

    // Add values of $station_meta as custom fields

    foreach ($station_meta as $key => $value) { // Cycle through the $station_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }

}

add_action('save_post', 'txpbs_save_events_meta', 1, 2); // save the custom fields
share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

Basically you need to add the html of the select dropdown at your function inventory_information() which is the function that actually displays the metabox:

// The Event Location Metabox

        function inventory_information() {
            global $post;

            // Noncename needed to verify where the data originated
            echo '<input type="hidden" name="inventorymeta_noncename" id="inventorymeta_noncename" value="' .
            wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

            // Get the location data if its already been entered
            $stocknum = get_post_meta($post->ID, '_dappcf_i_stocknum', true);
            $vin = get_post_meta($post->ID, '_dappcf_i_vin', true);
            $saleprice = get_post_meta($post->ID, '_dappcf_i_priceone', true);
            $internetprice = get_post_meta($post->ID, '_dappcf_i_pricetwo', true);
            $milage = get_post_meta($post->ID, '_dappcf_i_mileage', true);
            $carfaxurl = get_post_meta($post->ID, '_dappcf_i_carfaxurl', true);

            //here you add the dropdown as value if already set so you add something like
            $my_dropdown = get_post_meta($post->ID, '_dappcf_i_dropdown', true);

            // Echo out the fields
            echo '<p>Stock #: <input type="text" name="_dappcf_i_stocknum" value="' . $stocknum  . '" class="widefat" style="width:80px" /> &nbsp;&nbsp;&nbsp; 
                Milage: <input type="text" name="_dappcf_i_mileage" value="' . $milage  . '" class="widefat"  style="width:80px" /> &nbsp;&nbsp;&nbsp; 
                VIN: <input type="text" name="_dappcf_i_vin" value="' . $vin  . '" class="widefat"  style="width:200px" />';
            echo '<p>Sale Price: <input type="text" name="_dappcf_i_priceone" value="' . $saleprice  . '" class="widefat"  style="width:80px" /> &nbsp;&nbsp;&nbsp; 
                Internet Price: <input type="text" name="_dappcf_i_pricetwo" value="' . $internetprice  . '" class="widefat"  style="width:80px" />';
            echo '<p>CarFax url: <input type="text" name="_dappcf_i_carfaxurl" value="' . $carfaxurl  . '" class="widefat"  style="width:170px" />';

            //here you add the HTML of the dropdown you add something like
            echo '<p>Select menu: <select name="_dappcf_i_dropdown" class="widefat">';
            echo '<option value="1"'. $my_dropdown == "1" ? ' selected="selected"' : ''. '>' . 'Option 1'. '</option>';
            echo '<option value="2"'. $my_dropdown == "2" ? ' selected="selected"' : ''. '>' . 'Option 2'. '</option>'; 
            echo '<option value="3"'. $my_dropdown == "3" ? ' selected="selected"' : ''. '>' . 'Option 3'. '</option>'; 
            echo '<option value="4"'. $my_dropdown == "4" ? ' selected="selected"' : ''. '>' . 'Option 4'. '</option>'; 

            //add as many as you need here

            echo '</select>';
        }

and then just add that field to your metabox save function which is txpbs_save_events_meta so:

// Save the Metabox Data

        function txpbs_save_events_meta($post_id, $post) {

            // verify this came from the our screen and with proper authorization,
            // because save_post can be triggered at other times
            if ( !wp_verify_nonce( $_POST['inventorymeta_noncename'], plugin_basename(__FILE__) )) {
            return $post->ID;
            }

            // Is the user allowed to edit the post or page?
            if ( !current_user_can( 'edit_post', $post->ID ))
                return $post->ID;

            // OK, we're authenticated: we need to find and save the data
            // We'll put it into an array to make it easier to loop though.

            $station_meta['_dappcf_i_stocknum'] = $_POST['_dappcf_i_stocknum'];
            $station_meta['_dappcf_i_vin'] = $_POST['_dappcf_i_vin'];
            $station_meta['_dappcf_i_priceone'] = $_POST['_dappcf_i_priceone'];
            $station_meta['_dappcf_i_pricetwo'] = $_POST['_dappcf_i_pricetwo'];
            $station_meta['_dappcf_i_mileage'] = $_POST['_dappcf_i_mileage'];
            $station_meta['_dappcf_i_carfaxurl'] = $_POST['_dappcf_i_carfaxurl'];

            //here just add the dropdown field to the $station_meta array from the $_POST
            $station_meta['_dappcf_i_dropdown'] = $_POST['_dappcf_i_dropdown'];


            // Add values of $station_meta as custom fields

            foreach ($station_meta as $key => $value) { // Cycle through the $station_meta array!
                if( $post->post_type == 'revision' ) return; // Don't store custom data twice
                $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
                if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
                    update_post_meta($post->ID, $key, $value);
                } else { // If the custom field doesn't have a value
                    add_post_meta($post->ID, $key, $value);
                }
                if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
            }

        }

Hope this helps.

share|improve this answer
    
@בניית אתרים The select menu shows up as it should but none of the options are displaying. Any thoughts on what could be the issue? –  mattrepublic Feb 19 '11 at 13:23
    
Paste your code and I'll try to look for a reason. –  Bainternet Feb 19 '11 at 13:30
    
gist.github.com/835081 –  mattrepublic Feb 19 '11 at 14:25
1  
try this pastie.org/1582372 and also could have something to do with the fact that you keep opening <p> tags but never close them –  Bainternet Feb 19 '11 at 14:43
    
That was it! Thank you so much for your help. –  mattrepublic Feb 20 '11 at 13:46
show 1 more 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.