Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action('save_post', 'my_metabox_save');
- add_action( 'add_meta_boxes', 'my_metabox' );
- function my_metabox () {
- add_meta_box('my_metabox_id', "my test metabox", 'my_metabox_cb', 'portfolio', 'normal', 'high');
- }
- //Metabox callback function
- function my_metabox_cb () {
- wp_nonce_field( plugin_basename(__FILE__), 'myplugin_noncename' );
- $my_favorite_car = get_post_meta($_GET['post'], 'my-best-car', true);
- ?>
- <div class="my-metabox-field">
- <h2>my-car-type</h2>
- <?php
- $cars = array('car1', 'car2', 'car3', 'car4', 'car5', 'car6');
- foreach ($cars as $car) {
- echo '<input name="my-best-car" type="radio" onchange="javascript:document.post.submit()"';
- $option = 'id=" ' .$car . '"';
- $option = 'value="' . $car . '"';
- if ($category == $my_favorite_car) $option .= 'checked="checked"';
- $option .= '>';
- $option .= '<label for=" '.$car .' ">' . $car .' ';
- $option .= '</label>';
- echo $option;
- }
- ?>
- <?php
- if ($my_favorite_car == "car1") :
- ?>
- <div class="car1-info-box>
- <h2>car1 info </h2>
- ///my content for car 1 information
- <div>
- <?php
- endif;
- endif;
- ?>
- <?php
- if ($my_favorite_car == "car2") :
- ?>
- //my if else statement for all cars type car3, car4 shortened here. visibility of each div is toggle in front end using jQuery. So selection on each car in radio option make respective div visible for me.
- ////////////////////////////////////////////////////////////////////////////////////////////////////////
- <?php
- }
- function my_metabox_save ($post_id) {
- if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
- return $post_id;
- }
- if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
- return $post_id;
- if ( 'page' == $_POST['post_type'] ) {
- if ( !current_user_can( 'edit_page', $post_id ) )
- return $post_id;
- } else {
- if ( !current_user_can( 'edit_post', $post_id ) )
- return $post_id;
- }
- update_post_meta($post_id, 'my-best-car', $_POST['my-best-car'], true);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement