diff options
Property fields now get saved in the database and are called when vehicle is edited. Transferred cars display callback to another file.
2 files changed, 68 insertions, 2 deletions
| -rw-r--r-- | wp-content/plugins/dx-cars/car_info.php | 18 | ||||
| -rw-r--r-- | wp-content/plugins/dx-cars/class.cpt.php | 52 |
diff --git a/wp-content/plugins/dx-cars/car_info.php b/wp-content/plugins/dx-cars/car_info.php new file mode 100644 index 0000000..2138cc4 --- /dev/null +++ b/wp-content/plugins/dx-cars/car_info.php @@ -0,0 +1,18 @@ +<table> + <tr> + <td>Year:</td> + <td><input type="text" name="car-year" value="<?php echo(esc_textarea(get_post_meta( get_the_ID(), 'car-year', true ))) ?>"></td> + </tr> + <tr> + <td>Millage:</td> + <td><input type="text" name="car-millage" value="<?php echo(esc_textarea(get_post_meta( get_the_ID(), 'car-millage', true ))) ?>"></td> + </tr> + <tr> + <td>Price:</td> + <td><input type="text" name="car-price" value="<?php echo(esc_textarea(get_post_meta( get_the_ID(), 'car-price', true ))) ?>"></td> + </tr> + <tr> + <td>Photo:</td> + <td><input type="image" name="car-image" value="<?php echo(esc_textarea(get_post_meta( get_the_ID(), 'car-image', true ))) ?>"></td> + </tr> +</table> diff --git a/wp-content/plugins/dx-cars/class.cpt.php b/wp-content/plugins/dx-cars/class.cpt.php index 449927f..7e0c2b3 100644 --- a/wp-content/plugins/dx-cars/class.cpt.php +++ b/wp-content/plugins/dx-cars/class.cpt.php @@ -3,6 +3,7 @@ class Vehicles { function init() { add_action( 'init', array( $this, 'cpt_create' ) ); add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) ); + add_action( 'save_post', array( $this, 'car_info_save_meta' ), 1, 2 ); } public function labels() { @@ -19,7 +20,54 @@ class Vehicles { return $arr; } - function cpt_create() { - register_post_type( 'Vehicles', $this->labels() ); + function cpt_create( $post ) { + register_post_type( 'vehicles', $this->labels() ); + } + + function register_meta_boxes() { + add_meta_box( 'car-info', __( 'Car Information' ), array( $this, 'car_info_display_callback' ), 'vehicles' ); + } + + function car_info_display_callback( $post ) { + include 'car_info.php'; + } + + function car_info_save_meta( $post_id, $post ) { + // Return if the user doesn't have edit permissions. + if ( ! current_user_can( 'edit_post', $post_id ) ) { + return $post_id; + } + + // Now that we're authenticated, time to save the data. + // This sanitizes the data from the field and saves it into an array $events_meta. + $events_meta = array( + 'car-year' => esc_textarea( isset($_POST['car-year']) ? $_POST['car-year'] : null ), + 'car-millage' => esc_textarea( isset($_POST['car-millage']) ? $_POST['car-millage'] : null ), + 'car-price' => esc_textarea( isset($_POST['car-price']) ? $_POST['car-price'] : null ), + 'car-image' => esc_textarea( isset($_POST['car-image']) ? $_POST['car-image'] : null ), + ); + + // Cycle through the $events_meta array. + // Note, in this example we just have one item, but this is helpful if you have multiple. + foreach ( $events_meta as $key => $value ) { + + // Don't store custom data twice + if ( 'revision' === $post->post_type ) { + return; + } + + if ( get_post_meta( $post_id, $key, false ) ) { + // If the custom field already has a value, update it. + update_post_meta( $post_id, $key, $value ); + } else { + // If the custom field doesn't have a value, add it. + add_post_meta( $post_id, $key, $value ); + } + + if ( ! $value ) { + // Delete the meta key if there's no value + delete_post_meta( $post_id, $key ); + } + } } } |
