Advertisement
luisabarca

Metabox with sidebars dropdown

Nov 29th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Custom Sidebars
  4. */
  5.  
  6. add_action('add_meta_boxes', 'sidebar_meta_boxes');
  7.  
  8. function sidebar_meta_boxes()
  9. {
  10.     add_meta_box( 'sidebar_metabox-1', __( 'Custom sidebar' ), 'sidebar_meta_box_content', 'page', 'normal', 'high');
  11. }
  12.  
  13. function show_custom_sidebar($post_id = 0)
  14. {
  15.     global $post;
  16.    
  17.     // No post ID
  18.     if ($post_id < 1) {
  19.         $post_id = get_the_ID();
  20.     }
  21.    
  22.     $sidebar = get_post_meta($post_id, 'menu-sidebar', true);
  23.    
  24.     if ( !empty($sidebar) ) {
  25.         dynamic_sidebar($sidebar);        
  26.     }
  27. }
  28.  
  29. // }}}
  30. // {{{
  31.  
  32. function sidebar_meta_box_content()
  33. {
  34.     global $post;  
  35.     global $wp_registered_sidebars;
  36.    
  37.     $selected = get_post_meta($post->ID, 'menu-sidebar', true);
  38.    
  39.     wp_nonce_field('sidebar_save', 'sidebar_nonce');    
  40.     ?>    
  41.   <div class="jaxtag">
  42.   <div class="ajaxtag hide-if-no-js">
  43.     <table style="width: 100%">
  44.         <tr>
  45.             <td width="25%">
  46.             <label for="menusidebar"><?php _e('Menu sidebar') ?>:</label>
  47.             <label class="screen-reader-text" for="menusidebar"><?php _e('Menu sidebar') ?>:</label>
  48.             </td>
  49.             <td width="75%">
  50.             <select name="menusidebar" id="menusidebar">
  51.                 <?php foreach ($wp_registered_sidebars as $sidebar) : ?>
  52.                 <option value="<?php echo $sidebar['id'] ?>" <?php echo (($selected == $sidebar['id']) ? 'selected="selected"' : '') ?>><?php echo $sidebar['name'] ?></option>
  53.                 <?php endforeach ?>
  54.             </select>
  55.             </td>
  56.         </tr>
  57.     </table>
  58.   </div>  
  59.   </div>
  60.    
  61.     <?php
  62. }
  63.  
  64.  
  65. add_action('save_post', 'sidebar_save_postdata', 10, 2);
  66.  
  67. function sidebar_save_postdata( $post_id )
  68. {
  69.     global $wpdb;
  70.  
  71.     if ( !wp_verify_nonce( $_POST['sidebar_nonce'], 'sidebar_save' ) ) {
  72.         return $post_id;
  73.     }
  74.  
  75.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
  76.         return $post_id;
  77.     }
  78.  
  79.     // Check permissions ....
  80.  
  81.     /*
  82.      * Sidebar
  83.      */
  84.     $menusidebar = trim($_POST['menusidebar']);
  85.  
  86.     if ( !empty($menusidebar) ) {
  87.         update_post_meta($post_id, 'menu-sidebar', $menusidebar);
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement