Advertisement
terorama

WP / Custom taxonomies input panels

Mar 10th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2. add_action( 'init', 'create_theme_taxonomy', 0 );
  3.  
  4. function create_theme_taxonomy() {
  5.     if (!taxonomy_exists('theme')) {
  6.         register_taxonomy( 'theme', 'post', array( 'hierarchical' => false, 'label' => __('Theme'), 'query_var' => 'theme', 'rewrite' => array( 'slug' => 'theme' ) ) );
  7.  
  8.         wp_insert_term('Beauty', 'theme');
  9.         wp_insert_term('Dragons', 'theme');
  10.         wp_insert_term('Halloween', 'theme');
  11.     }
  12. }
  13.  
  14. function add_theme_box() {
  15.     add_meta_box('theme_box_ID', __('Theme'), 'your_styling_function', 'post', 'side', 'core');
  16. }  
  17.  
  18. function add_theme_menus() {
  19.  
  20.     if ( ! is_admin() )
  21.         return;
  22.  
  23.     add_action('admin_menu', 'add_theme_box');
  24.  
  25.    /* Use the save_post action to save new post data */
  26.     add_action('save_post', 'save_taxonomy_data');
  27. }
  28.  
  29. add_theme_menus();
  30.  
  31. // This function gets called in edit-form-advanced.php
  32. function your_styling_function($post) {
  33.  
  34.     echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="' .
  35.             wp_create_nonce( 'taxonomy_theme' ) . '" />';
  36.  
  37.      
  38.     // Get all theme taxonomy terms
  39.     $themes = get_terms('theme', 'hide_empty=0');
  40.  
  41. ?>
  42. <select name='post_theme' id='post_theme'>
  43.     <!-- Display themes as options -->
  44.     <?php
  45.         $names = wp_get_object_terms($post->ID, 'theme');
  46.         ?>
  47.         <option class='theme-option' value=''
  48.         <?php if (!count($names)) echo "selected";?>>None</option>
  49.         <?php
  50.     foreach ($themes as $theme) {
  51.         if (!is_wp_error($names) && !empty($names) && !strcmp($theme->slug, $names[0]->slug))
  52.             echo "<option class='theme-option' value='" . $theme->slug . "' selected>" . $theme->name . "</option>\n";
  53.         else
  54.             echo "<option class='theme-option' value='" . $theme->slug . "'>" . $theme->name . "</option>\n";
  55.     }
  56.    ?>
  57. </select>    
  58. <?php
  59. }
  60.  
  61. remove_meta_box('tagsdiv-theme','post','core');
  62.  
  63. function save_taxonomy_data($post_id) {
  64. // verify this came from our screen and with proper authorization.
  65.  
  66.     if ( !wp_verify_nonce( $_POST['taxonomy_noncename'], 'taxonomy_theme' )) {
  67.         return $post_id;
  68.     }
  69.  
  70.     // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
  71.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  72.         return $post_id;
  73.  
  74.    
  75.     // Check permissions
  76.     if ( 'page' == $_POST['post_type'] ) {
  77.         if ( !current_user_can( 'edit_page', $post_id ) )
  78.             return $post_id;
  79.     } else {
  80.         if ( !current_user_can( 'edit_post', $post_id ) )
  81.         return $post_id;
  82.     }
  83.  
  84.     // OK, we're authenticated: we need to find and save the data
  85.     $post = get_post($post_id);
  86.     if (($post->post_type == 'post') || ($post->post_type == 'page')) {
  87.            // OR $post->post_type != 'revision'
  88.            $theme = $_POST['post_theme'];
  89.        wp_set_object_terms( $post_id, $theme, 'theme' );
  90.         }
  91.     return $theme;
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement