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 have a pulldown menu in a custom write box that classification numbers. How can I populate a select box on the front end that will: a) be populated with all the possible values defined in the functions script b) have the value chosen in wp-admin be selected on page load in the pulldown menu?

This question piggybacks on my how to add a select menu to a custom meta box that was answered (perfectly) by בניית אתרים.

My cpt meta box code:

function inventory_information() {
    global $post;

    echo '<input type="hidden" name="inventorymeta_noncename" id="inventorymeta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    $stocknum = get_post_meta($post->ID, '_dappcf_i_stocknum', true);
    $vin = get_post_meta($post->ID, '_dappcf_i_vin', true);
    $my_dropdown = get_post_meta($post->ID, '_dappcf_i_dropdown', true);

    echo '<p>Stock #: <input type="text" name="_dappcf_i_stocknum" value="' . $stocknum  . '" class="widefat" style="width:80px" /> &nbsp;&nbsp;&nbsp; 
VIN: <input type="text" name="_dappcf_i_vin" value="' . $vin  . '" class="widefat"  style="width:200px" /></p>';

    echo '<p>Inventory class: <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>'; 
    echo '</select></p>';
}

function txpbs_save_events_meta($post_id, $post) {
    if ( !wp_verify_nonce( $_POST['inventorymeta_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }
    if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;

    $station_meta['_dappcf_i_stocknum'] = $_POST['_dappcf_i_stocknum'];
    $station_meta['_dappcf_i_vin'] = $_POST['_dappcf_i_vin'];
    $station_meta['_dappcf_i_dropdown'] = $_POST['_dappcf_i_dropdown'];

    foreach ($station_meta as $key => $value) {
if( $post->post_type == 'revision' ) return;
$value = implode(',', (array)$value); 
if(get_post_meta($post->ID, $key, FALSE)) {
    update_post_meta($post->ID, $key, $value);
} else {
    add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key);
    }

}

And I use this code to call my custom fields when displayed in an input field:

<?php echo get_post_meta($post->ID, '_dappcf_i_dropdown', true); ?>

Any direction is greatly appreciated.

share|improve this question
    
What does "be populated with all the possible values defined in the functions script" mean, where are all these possible values stored? –  t31os Mar 3 '11 at 10:41
    
It means that the values displayed in the pulldown are displayed on the front end. So value="1", value="2", value="3" are shown on the frontend pulldown's options as 1, 2, 3. The frontend should just echo the options that are available in the backend. Does that make sense @t31os ? –  mattrepublic Mar 4 '11 at 12:30
    
"the options that are available in the backend." Yes i understand that part, but where are these options to be pulled from(and based on what?), and is that in your above code? –  t31os Mar 4 '11 at 16:33
    
@t31os Sorry for the confusion. The code above is my code and that is custom meta field, that is _dappcf_i_dropdown is where the options are pulled from. –  mattrepublic Mar 4 '11 at 21:06
    
You're explicitly setting the possible values in the first function, 1, 2, 3 and 4.. the _dappcf_i_dropdown you're referring to is just a custom field name and any references to that name are just fetching a given post's meta value for that key. –  t31os Mar 19 '11 at 13:51

1 Answer 1

up vote 2 down vote accepted

Still not sure what exactly what you aiming for, but having come back to this question i wanted to at least try to offer a solution.

So try this out, and see if that's the kind of thing you had in mind with the dropdown... ?

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

function inventory_dropdown_opts() {
    return array(
        'Option 1' => 1,
        'Option 2' => 2,
        'Option 3' => 3,
        'Option 4' => 4,
    );
}

function inventory_information() {
    global $post;

    $stk_num = get_post_meta( $post->ID, '_dappcf_i_stocknum', true );
    $vin_num = get_post_meta( $post->ID, '_dappcf_i_vin', true );
    $drp_dwn = get_post_meta( $post->ID, '_dappcf_i_dropdown', true );
    if( empty( $drp_dwn ) )
        $drp_dwn = 0;

    wp_nonce_field( 'inventory_nonce', 'inventorymeta_noncename' );
    ?>
    <p>
        <label>Stock #</label> 
        <input type="text" name="_dappcf_i_stocknum" value="<?php echo $stk_num; ?>" class="small-text" />
    </p>
    <p>
        <label>VIN</label> 
        <input type="text" name="_dappcf_i_vin" value="<?php echo $vin_num; ?>" class="widefat" /></p>
    <p>
        <label>Inventory class</label> 
        <select name="_dappcf_i_dropdown" class="widefat">
        <?php foreach( inventory_dropdown_opts() as $text => $value ) : ?>
            <option value="<?php echo $value; ?>"<?php selected( $value == $drp_dwn ); ?>><?php echo $text; ?></option>
        <?php endforeach; ?>
        </select>
    </p>
    <?php
}

add_action('save_post', 'txpbs_save_events_meta', 1, 2);

function txpbs_save_events_meta( $post_id, $post ) {
    if( 
        !wp_verify_nonce( $_POST['inventorymeta_noncename'], 'inventory_nonce' ) || 
        !current_user_can( 'edit_post', $post_id ) || 
        $post->post_type == 'revision'
        )
        return;

    foreach( array( '_dappcf_i_stocknum', '_dappcf_i_vin', '_dappcf_i_dropdown' ) as $field ) {
        if( isset( $_POST[$field] ) ) {
            if( '_dappcf_i_dropdown' == $field ) {
                if( in_array( $_POST[$field], inventory_dropdown_opts() ) )
                    update_post_meta( $post_id, $field, $_POST[$field] );
                continue;
            }
            update_post_meta( $post_id, $field, $_POST[$field] );
            continue;
        }
        delete_post_meta( $post_id, $key );
    }
}

Follow-up #1
If you need to show the dropdown in the front end, just do it the same way i did inside the inventory_information function, like so..

    <select name="add-a-name-here" class="widefat">
    <?php foreach( inventory_dropdown_opts() as $text => $value ) : ?>
        <option value="<?php echo $value; ?>"><?php echo $text; ?></option>
    <?php endforeach; ?>
    </select>

Your dropdown options are defined in the inventory_dropdown_opts function, so you can use the same both front and admin side, and see the same data.

share|improve this answer
    
I appreciate your time in helping solve this. As you said there is some confusion for what I was trying to accomplish so for that I apologize. I'm trying to display the pulldown variables in the custom meta field on the frontend, so in single-inventory.php as a pulldown menu. So if the pulldown in wp-admin is for option 1, 2, 3, 4 the pulldown on the front is for option 1, 2, 3, 4. I hope this clarifies things. :) –  mattrepublic Mar 25 '11 at 18:32
    
Have updated my answer. :) –  t31os Mar 31 '11 at 17:40
    
Awesome. Thanks @t31os for your help, I was really really getting stuck on this. :) –  mattrepublic Apr 7 '11 at 0:48

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.