1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
 * CUSTOM POST TYPES
 */


// Registers the new post type and taxonomy

function inventory_posttype() {
    register_post_type( 'inventory',
        array(
            'labels' => array(
                'name' => __( 'Inventory' ),
                'singular_name' => __( 'Inventory' ),
                'add_new' => __( 'Add New Inventory' ),
                'add_new_item' => __( 'Add New Inventory' ),
                'edit_item' => __( 'Edit Inventory' ),
                'new_item' => __( 'Add New Inventory' ),
                'view_item' => __( 'View Inventory' ),
                'search_items' => __( 'Search Inventory' ),
                'not_found' => __( 'No inventory found' ),
                'not_found_in_trash' => __( 'No inventory found in trash' )
            ),
            'public' => true,
            'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
            'capability_type' => 'post',
			'has_archive' => 'inventory',
            'rewrite' => array("slug" => "inventory"), // Permalinks format
            'menu_position' => null,
            'register_meta_box_cb' => 'add_inventory_metaboxes'
        )
    );
}
add_action( 'init', 'inventory_posttype' );
//add_action('admin_init', 'flush_rewrite_rules');


// 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);
	
            //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"  style="width:170px" >';
            echo '<option value="1"'; if  ($my_dropdown == "1"){echo ' selected="selected"';} echo '>Option 1</option>';
            echo '<option value="2"'; if  ($my_dropdown == "2"){echo ' selected="selected"';} echo '>Option 2</option>'; 
            echo '<option value="3"'; if  ($my_dropdown == "3"){echo ' selected="selected"';} echo '>Option 3</option>'; 
            echo '<option value="4"'; if  ($my_dropdown == "4"){echo ' selected="selected"';} echo '>Option 4</option>';  
            echo '</select>';
}

// 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
    }

}

add_action('save_post', 'txpbs_save_events_meta', 1, 2); // save the custom fields

?>