Advertisement
duck__boy

Custom Taxonomy Dropdown in Post edit screen

Sep 13th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package WordPress
  4.  * @subpackage DyneDrewett
  5.  */
  6.  
  7. /** Remove the 'Sectors' meta box from the edit post screen for the service post type */
  8. add_action('admin_head', 'remove_sectors_taxonomy_box');
  9. function remove_sectors_taxonomy_box(){
  10.    
  11.     remove_meta_box('tagsdiv-sector', POST_TYPE_SERVICE, 'side');
  12.    
  13. }
  14.  
  15. /** Add a custom matabox for placing a service in a sector, and save the options when the user presses 'Publish'/'Update' */
  16. if(current_user_can('manage_categories')) :
  17.    
  18.     /** Add the sectors dropdown */
  19.     add_action('admin_menu', 'add_sectors_dropdown');
  20.    
  21.     /** Save the sectors taxonomy data */
  22.     add_action('save_post', 'save_sectors_data');
  23.  
  24. endif;
  25.  
  26. /**
  27.  * Create the custom 'Sector' meta box
  28.  */
  29. function add_sectors_dropdown(){
  30.    
  31.     add_meta_box('sectors-dropdown', __('Sector'), 'do_sectors_dropdown', POST_TYPE_SERVICE, 'side', 'core');
  32.    
  33. }
  34.  
  35. /**
  36.  * Populate the custom 'Sector' meta box
  37.  */
  38. function do_sectors_dropdown(){
  39.    
  40.     global $post;
  41.    
  42.     /** Create a security nonce for this dorpdown */
  43.     echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="'.wp_create_nonce('taxonomy_sector').'" />';
  44.  
  45.     /** Get all sector taxonomy terms */
  46.     $sectors = get_terms(TAXONOMY_SECTOR, 'hide_empty=0');
  47.    
  48.     /** Get all of this object's terms that are in the 'sector' taxonomy */
  49.     $selected_sectors = wp_get_object_terms($post->ID, TAXONOMY_SECTOR);
  50.  
  51.     $dropdown = "\n\n".'<select name="sector" id="sector">'."\n";
  52.     $dropdown.= "\t".'<option id="sector-none" value="no-sector">Select a Sector</option>'."\n";
  53.     foreach($sectors as $sector) :
  54.         $selected = (!is_wp_error($selected_sectors) && !empty($selected_sectors) && !strcmp($sector->slug, $selected_sectors[0]->slug)) ? ' selected="true"' : false;
  55.         $dropdown.= "\t".'<option id="sector-'.$sector->term_id.'" value="'.$sector->slug.'"'.$selected.'>'.$sector->name .'</option>'."\n";
  56.     endforeach;
  57.     $dropdown.= '<select name="sector" id="sector">'."\n\n";
  58.    
  59.     echo $dropdown;
  60.    
  61. }
  62.  
  63. /**
  64.  * Save the term that the user selects from the custom 'Sector' meta box
  65.  *
  66.  * @param required integer $post_id The id of the post being edited
  67.  * @return false|string $sector The sector that the user selected
  68.  */
  69. function save_sectors_data($post_id) {
  70.  
  71.     /** Check the security nonce to ensure we have the proper authorisation  */
  72.     if(!wp_verify_nonce($_POST['taxonomy_noncename'], 'taxonomy_sector')) :
  73.         return false;
  74.     endif;
  75.    
  76.     /** Ensure that the user has the correct permissions */
  77.     if(!current_user_can('edit_post', $post_id)) :
  78.         return false;
  79.     endif;
  80.  
  81.     /** Check if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything */
  82.     if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) :
  83.         return false;
  84.     endif;
  85.  
  86.     /** Now save the data against the correct taxonomy */
  87.     $post = get_post($post_id);
  88.     if($post->post_type === POST_TYPE_SERVICE) :
  89.         $sector = $_POST[TAXONOMY_SECTOR];
  90.         wp_set_object_terms($post_id, $sector, TAXONOMY_SECTOR);
  91.     endif;
  92.    
  93.     return $sector;
  94.  
  95. }
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement