Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @package WordPress
- * @subpackage DyneDrewett
- */
- /** Remove the 'Sectors' meta box from the edit post screen for the service post type */
- add_action('admin_head', 'remove_sectors_taxonomy_box');
- function remove_sectors_taxonomy_box(){
- remove_meta_box('tagsdiv-sector', POST_TYPE_SERVICE, 'side');
- }
- /** Add a custom matabox for placing a service in a sector, and save the options when the user presses 'Publish'/'Update' */
- if(current_user_can('manage_categories')) :
- /** Add the sectors dropdown */
- add_action('admin_menu', 'add_sectors_dropdown');
- /** Save the sectors taxonomy data */
- add_action('save_post', 'save_sectors_data');
- endif;
- /**
- * Create the custom 'Sector' meta box
- */
- function add_sectors_dropdown(){
- add_meta_box('sectors-dropdown', __('Sector'), 'do_sectors_dropdown', POST_TYPE_SERVICE, 'side', 'core');
- }
- /**
- * Populate the custom 'Sector' meta box
- */
- function do_sectors_dropdown(){
- global $post;
- /** Create a security nonce for this dorpdown */
- echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="'.wp_create_nonce('taxonomy_sector').'" />';
- /** Get all sector taxonomy terms */
- $sectors = get_terms(TAXONOMY_SECTOR, 'hide_empty=0');
- /** Get all of this object's terms that are in the 'sector' taxonomy */
- $selected_sectors = wp_get_object_terms($post->ID, TAXONOMY_SECTOR);
- $dropdown = "\n\n".'<select name="sector" id="sector">'."\n";
- $dropdown.= "\t".'<option id="sector-none" value="no-sector">Select a Sector</option>'."\n";
- foreach($sectors as $sector) :
- $selected = (!is_wp_error($selected_sectors) && !empty($selected_sectors) && !strcmp($sector->slug, $selected_sectors[0]->slug)) ? ' selected="true"' : false;
- $dropdown.= "\t".'<option id="sector-'.$sector->term_id.'" value="'.$sector->slug.'"'.$selected.'>'.$sector->name .'</option>'."\n";
- endforeach;
- $dropdown.= '<select name="sector" id="sector">'."\n\n";
- echo $dropdown;
- }
- /**
- * Save the term that the user selects from the custom 'Sector' meta box
- *
- * @param required integer $post_id The id of the post being edited
- * @return false|string $sector The sector that the user selected
- */
- function save_sectors_data($post_id) {
- /** Check the security nonce to ensure we have the proper authorisation */
- if(!wp_verify_nonce($_POST['taxonomy_noncename'], 'taxonomy_sector')) :
- return false;
- endif;
- /** Ensure that the user has the correct permissions */
- if(!current_user_can('edit_post', $post_id)) :
- return false;
- endif;
- /** Check if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything */
- if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) :
- return false;
- endif;
- /** Now save the data against the correct taxonomy */
- $post = get_post($post_id);
- if($post->post_type === POST_TYPE_SERVICE) :
- $sector = $_POST[TAXONOMY_SECTOR];
- wp_set_object_terms($post_id, $sector, TAXONOMY_SECTOR);
- endif;
- return $sector;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement