Advertisement
websupporter

WP Term Meta data: Category pictures

Nov 5th, 2015
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. <?php
  2.     /**
  3.      * Plugin Name: Images for Categories
  4.      * Author: Websupporter
  5.      **/
  6.  
  7.     add_action( 'category_add_form_fields', 'fifc_add_taxonomy_picture', 10, 2 );
  8.     add_action( 'category_edit_form_fields', 'fifc_add_taxonomy_picture', 10, 2 );
  9.     function fifc_add_taxonomy_picture(){
  10.         $args = func_get_args();
  11.         if( is_string( $args[0] ) ){
  12.             $taxonomy = $args[0];
  13.         } else {
  14.             $taxonomy = $args[1];
  15.             $term = $args[0];
  16.         }
  17.  
  18.         if( ! isset( $term ) ):
  19.         ?>
  20.         <div class="form-field">
  21.             <label for="thumbnail">Kategorie-Bild</label>
  22.             <div id="thumbnail">
  23.  
  24.                 <button class="button" id="thumbnail_mediathek">&Ouml;ffnen</button>
  25.             </div>
  26.         </div>
  27.         <?php
  28.         else:
  29.         ?>
  30.         <tr class="form-field">
  31.             <th scope="row"><label for="thumbnail">Kategorie-Bild</label></th>
  32.             <td>
  33.                 <div id="thumbnail">
  34.                 <?php
  35.                     $thumbnail = get_term_meta( $term->term_id, '_thumbnail_id', true );
  36.                     if( $thumbnail )
  37.                         echo wp_get_attachment_image( $thumbnail );
  38.                 ?>
  39.                     <button class="button" id="thumbnail_mediathek">&Ouml;ffnen</button>
  40.                 </div>
  41.                 <p class="description">
  42.                     Hier kann ein Kategoriebild hinzugef&uuml;gt werden.
  43.                 </p>
  44.             </td>
  45.         </tr>
  46.         <?php
  47.         endif;
  48.     }
  49.  
  50.     add_action( 'edited_terms', 'fifc_edited_terms' );
  51.     add_action( 'created_term', 'fifc_edited_terms' );
  52.     function fifc_edited_terms( $term_id ){
  53.         if( isset( $_POST['_thumbnail_id'] ) )
  54.             update_term_meta( $term_id, '_thumbnail_id', (int) $_POST['_thumbnail_id'] );      
  55.  
  56.     }
  57.     add_action( 'admin_enqueue_scripts', 'fifc_tax_term_script' );
  58.     function fifc_tax_term_script( $hook ){
  59.         if( !in_array( $hook, array( 'edit-tags.php' ) ) )
  60.             return;
  61.  
  62.         wp_enqueue_media();
  63.         wp_enqueue_script( 'fifc-admin-script', plugins_url( '/script.js', __FILE__ ), array( 'jquery' ) );
  64.     }
  65.  
  66.  
  67.  
  68.     function fifc_has_taxonomy_thumbnail( $term_id = null ){
  69.         if( null == $term_id ){
  70.             $queried_object = get_queried_object();
  71.             if( ! isset( $queried_object->term_id ) )
  72.                 return false;
  73.  
  74.             $term_id =  $queried_object->term_id;
  75.         }
  76.  
  77.         $thumbnail = get_term_meta( $term_id, '_thumbnail_id', true );
  78.         return ( ! empty( $thumbnail ) );
  79.     }
  80.  
  81.     function fifc_get_the_taxonomy_thumbnail( $term_id = null, $size = 'thumbnail', $icon = false, $attr = array() ){
  82.         if( null == $term_id ){
  83.             $queried_object = get_queried_object();
  84.             if( ! isset( $queried_object->term_id ) )
  85.                 return false;
  86.  
  87.             $term_id =  $queried_object->term_id;
  88.         }
  89.  
  90.         $thumbnail = get_term_meta( $term_id, '_thumbnail_id', true );
  91.         if( empty( $thumbnail ) )
  92.             return false;
  93.  
  94.         return wp_get_attachment_image( $thumbnail, $size, $icon, $attr );
  95.     }
  96.         function fifc_the_taxonomy_thumbnail( $term_id = null, $size = 'thumbnail', $icon = false, $attr = array() ){
  97.             echo fifc_get_the_taxonomy_thumbnail( $term_id, $size, $icon, $attr );
  98.         }
  99. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement