Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: Images for Categories
- * Author: Websupporter
- **/
- add_action( 'category_add_form_fields', 'fifc_add_taxonomy_picture', 10, 2 );
- add_action( 'category_edit_form_fields', 'fifc_add_taxonomy_picture', 10, 2 );
- function fifc_add_taxonomy_picture(){
- $args = func_get_args();
- if( is_string( $args[0] ) ){
- $taxonomy = $args[0];
- } else {
- $taxonomy = $args[1];
- $term = $args[0];
- }
- if( ! isset( $term ) ):
- ?>
- <div class="form-field">
- <label for="thumbnail">Kategorie-Bild</label>
- <div id="thumbnail">
- <button class="button" id="thumbnail_mediathek">Öffnen</button>
- </div>
- </div>
- <?php
- else:
- ?>
- <tr class="form-field">
- <th scope="row"><label for="thumbnail">Kategorie-Bild</label></th>
- <td>
- <div id="thumbnail">
- <?php
- $thumbnail = get_term_meta( $term->term_id, '_thumbnail_id', true );
- if( $thumbnail )
- echo wp_get_attachment_image( $thumbnail );
- ?>
- <button class="button" id="thumbnail_mediathek">Öffnen</button>
- </div>
- <p class="description">
- Hier kann ein Kategoriebild hinzugefügt werden.
- </p>
- </td>
- </tr>
- <?php
- endif;
- }
- add_action( 'edited_terms', 'fifc_edited_terms' );
- add_action( 'created_term', 'fifc_edited_terms' );
- function fifc_edited_terms( $term_id ){
- if( isset( $_POST['_thumbnail_id'] ) )
- update_term_meta( $term_id, '_thumbnail_id', (int) $_POST['_thumbnail_id'] );
- }
- add_action( 'admin_enqueue_scripts', 'fifc_tax_term_script' );
- function fifc_tax_term_script( $hook ){
- if( !in_array( $hook, array( 'edit-tags.php' ) ) )
- return;
- wp_enqueue_media();
- wp_enqueue_script( 'fifc-admin-script', plugins_url( '/script.js', __FILE__ ), array( 'jquery' ) );
- }
- function fifc_has_taxonomy_thumbnail( $term_id = null ){
- if( null == $term_id ){
- $queried_object = get_queried_object();
- if( ! isset( $queried_object->term_id ) )
- return false;
- $term_id = $queried_object->term_id;
- }
- $thumbnail = get_term_meta( $term_id, '_thumbnail_id', true );
- return ( ! empty( $thumbnail ) );
- }
- function fifc_get_the_taxonomy_thumbnail( $term_id = null, $size = 'thumbnail', $icon = false, $attr = array() ){
- if( null == $term_id ){
- $queried_object = get_queried_object();
- if( ! isset( $queried_object->term_id ) )
- return false;
- $term_id = $queried_object->term_id;
- }
- $thumbnail = get_term_meta( $term_id, '_thumbnail_id', true );
- if( empty( $thumbnail ) )
- return false;
- return wp_get_attachment_image( $thumbnail, $size, $icon, $attr );
- }
- function fifc_the_taxonomy_thumbnail( $term_id = null, $size = 'thumbnail', $icon = false, $attr = array() ){
- echo fifc_get_the_taxonomy_thumbnail( $term_id, $size, $icon, $attr );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement