Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.92 KB | None | 0 0
  1. add_action( 'init', 'create_theme_taxonomy', 0 );
  2.  
  3. function create_theme_taxonomy() {
  4.     if (!is_taxonomy('theme')) {
  5.         register_taxonomy( 'theme', 'post', array( 'hierarchical' => false, 'label' => __('Theme'), 'query_var' => 'theme', 'rewrite' => array( 'slug' => 'theme' ) ) );
  6.  
  7.         wp_insert_term('Beauty', 'theme');
  8.         wp_insert_term('Dragons', 'theme');
  9.         wp_insert_term('Halloween', 'theme');
  10.     }
  11. }
  12.  
  13. function add_theme_box() {
  14.     add_meta_box('theme_box_ID', __('Theme'), 'your_styling_function', 'post', 'side', 'core');
  15. }  
  16.  
  17. function add_theme_menus() {
  18.  
  19.     if ( ! is_admin() )
  20.         return;
  21.  
  22.     add_action('admin_menu', 'add_theme_box');
  23.    
  24.     /* Use the save_post action to save new post data */
  25.     add_action('save_post', 'save_taxonomy_data');
  26. }
  27.  
  28. add_theme_menus();
  29.  
  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. add_action( 'admin_head', 'remove_my_meta_boxen' );
  62.  
  63. function remove_my_meta_boxen() {
  64.     remove_meta_box('tagsdiv-theme','post','side');
  65. }
  66.  
  67.  
  68.  
  69. function save_taxonomy_data($post_id) {
  70. // verify this came from our screen and with proper authorization.
  71.  
  72.     if ( !wp_verify_nonce( $_POST['taxonomy_noncename'], 'taxonomy_theme' )) {
  73.         return $post_id;
  74.     }
  75.  
  76.     // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
  77.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  78.         return $post_id;
  79.  
  80.  
  81.     // Check permissions
  82.     if ( 'page' == $_POST['post_type'] ) {
  83.         if ( !current_user_can( 'edit_page', $post_id ) )
  84.             return $post_id;
  85.     } else {
  86.         if ( !current_user_can( 'edit_post', $post_id ) )
  87.         return $post_id;
  88.     }
  89.  
  90.     // OK, we're authenticated: we need to find and save the data
  91.     $post = get_post($post_id);
  92.     if (($post->post_type == 'post') || ($post->post_type == 'page')) {
  93.            // OR $post->post_type != 'revision'
  94.            $theme = $_POST['post_theme'];
  95.        wp_set_object_terms( $post_id, $theme, 'theme' );
  96.         }
  97.     return $theme;
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement