Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- 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... =)
- */
- //extra field in taxonomy: segmento
- add_action ('segmento_edit_form_fields', 'segmento_tax_meta_fields'); //hook {tax-name}_edit_form_fields
- add_action('segmento_add_form_fields','segmento_tax_meta_fields'); //hook {tax-name}_add_form_fields
- function segmento_tax_meta_fields( $tag ) { //check for existing featured ID
- $t_id = $tag->term_id;
- $Tax_meta = get_option("segmento_term_$t_id");
- echo '<tr class="form-field">';
- echo ' <th scope="row" valign="top"><label for="category_foto">Imagem</label></th>';
- echo ' <td>';
- echo ' <input type="text" name="segmento_tax_meta[image]" id="segmento_tax_meta_image" size="25" style="width:60%;" value="';
- echo ($Tax_meta['image']) ? $Tax_meta['image'] : '';
- echo '">';
- echo ' <input id="segmento_upload_file_button" type="button" class="button-primary" value="Enviar Imagem" style="width:100px" />';
- 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>';
- echo ' </td>';
- echo '</tr>';
- }
- // save extra taxonomy extra fields hook
- add_action ('edited_segmento', 'segmento_tax_meta_fileds_save'); //hook edited_{tax-name}
- add_action('created_segmento','segmento_tax_meta_fileds_save'); //hook created_{tax-name}
- // save extra taxonomy extra fields callback function
- function segmento_tax_meta_fileds_save( $term_id ) {
- if ( isset( $_POST['segmento_tax_meta'] ) ) {
- $t_id = $term_id;
- $Tax_meta = get_option( "segmento_term_$t_id" );
- $Tax_keys = array_keys($_POST['segmento_tax_meta']);
- foreach ($Tax_keys as $key){
- if (isset($_POST['segmento_tax_meta'][$key])){
- $Tax_meta[$key] = $_POST['segmento_tax_meta'][$key];
- }
- }
- //save the option array
- update_option( "segmento_term_$t_id", $Tax_meta );
- }
- }
- add_action('admin_print_scripts', 'segmento_mediauploader_scripts');
- function segmento_mediauploader_scripts() {
- wp_enqueue_script('media-upload');
- wp_enqueue_script('thickbox');
- }
- add_action('admin_print_styles', 'segmento_mediauploader_admin_styles');
- function segmento_mediauploader_admin_styles() {
- wp_enqueue_style('thickbox');
- }
- // native media uploader script call
- add_action( 'admin_head', 'segmento_metabox_uploader_script' );
- function segmento_metabox_uploader_script() {
- global $current_screen;
- if($current_screen->taxonomy == "segmento"):
- ?>
- <script type="text/javascript">
- jQuery(document).ready(function() {
- var formfield;
- var header_clicked = false;
- jQuery( '#segmento_upload_file_button' ).click( function() {
- formfield = jQuery( '#segmento_tax_meta_image' ).attr( 'name' );
- tb_show( 'Segmento: Imagem', 'media-upload.php?type=image&TB_iframe=true' );
- header_clicked = true;
- return false;
- });
- // Guarda o uploader original
- window.original_send_to_editor = window.send_to_editor;
- // Sobrescreve a função nativa e preenche o campo com a URL
- window.send_to_editor = function( html ) {
- if ( header_clicked ) {
- fileurl = jQuery( html ).attr( 'href' );
- imgsrc = jQuery( 'img', html ).attr( 'src' ); //console.log(imgsrc);
- jQuery( '#segmento_tax_meta_image' ).val( imgsrc );
- header_clicked = false;
- tb_remove();
- }
- else
- {
- window.original_send_to_editor( html );
- }
- }
- });
- </script>
- <?php
- endif;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment