miriamdepaula

WordPress: Custom Field in taxonomies- using native uploader

Jan 12th, 2012
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.02 KB | None | 0 0
  1. <?php
  2. /*
  3. OBSERVAÇÃO: Usei o nome "segmento" como taxonomia. Para usar este código para a sua taxonomia, basta fazer um FIND & REPLACE no seu editor de códigos preferido para procurar e substituir o nome "segmento" pelo nome da sua taxonomia... =)
  4.  
  5. */
  6.  
  7. //extra field in taxonomy: segmento
  8. add_action ('segmento_edit_form_fields', 'segmento_tax_meta_fields'); //hook {tax-name}_edit_form_fields
  9. add_action('segmento_add_form_fields','segmento_tax_meta_fields'); //hook {tax-name}_add_form_fields
  10.  
  11. function segmento_tax_meta_fields( $tag ) {    //check for existing featured ID
  12.  
  13.     $t_id = $tag->term_id;
  14.         $Tax_meta = get_option("segmento_term_$t_id");
  15.  
  16.     echo '<tr class="form-field">';
  17.     echo '  <th scope="row" valign="top"><label for="category_foto">Imagem</label></th>';
  18.     echo '      <td>';
  19.     echo '          <input type="text" name="segmento_tax_meta[image]" id="segmento_tax_meta_image" size="25" style="width:60%;" value="';
  20.     echo ($Tax_meta['image']) ? $Tax_meta['image'] : '';
  21.     echo '">';
  22.     echo '          <input id="segmento_upload_file_button" type="button" class="button-primary" value="Enviar Imagem" style="width:100px" />';
  23.     echo '          <p><em>Clique no botão <strong>"Enviar Imagem"</strong> para fazer o upload de uma imagem. Ao terminar, clique no botão <strong>"Inserir no post"</strong>.</em></p>';
  24.     echo '      </td>';
  25.     echo '</tr>';
  26.    
  27. }
  28.  
  29. // save extra taxonomy extra fields hook
  30. add_action ('edited_segmento', 'segmento_tax_meta_fileds_save'); //hook edited_{tax-name}
  31. add_action('created_segmento','segmento_tax_meta_fileds_save'); //hook created_{tax-name}
  32.  
  33. // save extra taxonomy extra fields callback function
  34. function segmento_tax_meta_fileds_save( $term_id ) {
  35.     if ( isset( $_POST['segmento_tax_meta'] ) ) {
  36.         $t_id = $term_id;
  37.         $Tax_meta = get_option( "segmento_term_$t_id" );
  38.         $Tax_keys = array_keys($_POST['segmento_tax_meta']);
  39.             foreach ($Tax_keys as $key){
  40.             if (isset($_POST['segmento_tax_meta'][$key])){
  41.                 $Tax_meta[$key] = $_POST['segmento_tax_meta'][$key];
  42.             }
  43.         }
  44.         //save the option array
  45.         update_option( "segmento_term_$t_id", $Tax_meta );
  46.     }
  47. }
  48.  
  49. add_action('admin_print_scripts', 'segmento_mediauploader_scripts');
  50. function segmento_mediauploader_scripts() {
  51.     wp_enqueue_script('media-upload');
  52.     wp_enqueue_script('thickbox');
  53. }
  54.  
  55. add_action('admin_print_styles', 'segmento_mediauploader_admin_styles');
  56. function segmento_mediauploader_admin_styles() {
  57.     wp_enqueue_style('thickbox');
  58. }
  59.  
  60. // native media uploader script call
  61. add_action( 'admin_head', 'segmento_metabox_uploader_script' );
  62. function segmento_metabox_uploader_script() {
  63.     global $current_screen;
  64.     if($current_screen->taxonomy == "segmento"):
  65.  
  66. ?>
  67.     <script type="text/javascript">
  68.         jQuery(document).ready(function() {
  69.  
  70.             var formfield;
  71.             var header_clicked = false;
  72.  
  73.             jQuery( '#segmento_upload_file_button' ).click( function() {
  74.                 formfield = jQuery( '#segmento_tax_meta_image' ).attr( 'name' );
  75.                 tb_show( 'Segmento: Imagem', 'media-upload.php?type=image&TB_iframe=true' );
  76.                 header_clicked = true;
  77.  
  78.                 return false;
  79.             });
  80.  
  81.             // Guarda o uploader original
  82.             window.original_send_to_editor = window.send_to_editor;
  83.  
  84.             // Sobrescreve a função nativa e preenche o campo com a URL
  85.             window.send_to_editor = function( html ) {
  86.                 if ( header_clicked ) {
  87.                    
  88.                     fileurl = jQuery( html ).attr( 'href' );
  89.             imgsrc = jQuery( 'img', html ).attr( 'src' ); //console.log(imgsrc);                   
  90.                    
  91.                     jQuery( '#segmento_tax_meta_image' ).val( imgsrc );
  92.                    
  93.                     header_clicked = false;
  94.                     tb_remove();
  95.                 }
  96.                 else
  97.                 {
  98.                     window.original_send_to_editor( html );
  99.                 }
  100.             }
  101.            
  102.  
  103.         });
  104.        
  105.   </script>
  106. <?php
  107.     endif;
  108. }
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment