Advertisement
Guest User

My-metabox-car

a guest
Dec 22nd, 2012
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. add_action('save_post', 'my_metabox_save');
  4. add_action( 'add_meta_boxes', 'my_metabox' );
  5.  
  6. function my_metabox () {
  7.  
  8. add_meta_box('my_metabox_id', "my test metabox", 'my_metabox_cb', 'portfolio', 'normal', 'high');
  9.  
  10. }
  11.  
  12.  
  13. //Metabox callback function
  14. function my_metabox_cb () {
  15.  
  16. wp_nonce_field( plugin_basename(__FILE__), 'myplugin_noncename' );
  17. $my_favorite_car = get_post_meta($_GET['post'], 'my-best-car', true);
  18.  
  19. ?>
  20.  
  21. <div class="my-metabox-field">
  22. <h2>my-car-type</h2>
  23.  
  24. <?php
  25.  
  26. $cars = array('car1', 'car2', 'car3', 'car4', 'car5', 'car6');
  27.  
  28. foreach ($cars as $car) {
  29. echo '<input name="my-best-car" type="radio" onchange="javascript:document.post.submit()"';
  30. $option = 'id=" ' .$car . '"';
  31. $option = 'value="' . $car . '"';
  32.  
  33. if ($category == $my_favorite_car) $option .= 'checked="checked"';
  34. $option .= '>';
  35. $option .= '<label for=" '.$car .' ">' . $car .' ';
  36. $option .= '</label>';
  37. echo $option;
  38.  
  39. }
  40.  
  41. ?>
  42.  
  43. <?php
  44.  
  45. if ($my_favorite_car == "car1") :
  46.  
  47. ?>
  48. <div class="car1-info-box>
  49. <h2>car1 info </h2>
  50.  
  51.  
  52. ///my content for car 1 information
  53.  
  54.  
  55. <div>
  56. <?php
  57.  
  58. endif;
  59.  
  60. endif;
  61.  
  62. ?>
  63.  
  64. <?php
  65.  
  66. if ($my_favorite_car == "car2") :
  67.  
  68. ?>
  69.  
  70. //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.
  71.  
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  73.  
  74. <?php
  75.  
  76. }
  77.  
  78. function my_metabox_save ($post_id) {
  79.  
  80. if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
  81. return $post_id;
  82. }
  83.  
  84. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  85. return $post_id;
  86.  
  87. if ( 'page' == $_POST['post_type'] ) {
  88. if ( !current_user_can( 'edit_page', $post_id ) )
  89. return $post_id;
  90. } else {
  91. if ( !current_user_can( 'edit_post', $post_id ) )
  92. return $post_id;
  93. }
  94.  
  95. update_post_meta($post_id, 'my-best-car', $_POST['my-best-car'], true);
  96.  
  97.  
  98. }
  99.  
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement