Last active
December 3, 2022 06:53
-
-
Save davidchc/d26c59b2046345b72c63fa043ca674b0 to your computer and use it in GitHub Desktop.
Adicionar imagem numa taxonomia personalizada - WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(function($){ | |
//Clicar no botão para selecionar imagem | |
$(".add_taxonomy_image").on('click', function(event) { | |
event.preventDefault(); | |
$this = $(this); | |
var frame = wp.media(); | |
frame.open(); | |
frame.on( "select", function() { | |
// Grab the selected attachment. | |
var attachment = frame.state().get("selection").first(); | |
var attachmentUrl = attachment.attributes.url; | |
var attachmentId = attachment.attributes.id; | |
//pega o id da imagem e adiciona no campo | |
$("#taxonomy_image_id").val(attachmentId); | |
//adiciona a imagem de preview e o botão para limapar a imagem | |
$("#image").html('<img src="'+attachmentUrl+'" width="100" /> <br /><button type="button" class="button delete_taxonomy_image">Remover Imagem</button>'); | |
frame.close(); | |
}); | |
}); | |
/*Limita os campos da imagem*/ | |
$(document).on('click', '.delete_taxonomy_image', function(event) { | |
event.preventDefault(); | |
$("#taxonomy_image_id").val(''); | |
$("#image").html(''); | |
}); | |
//Limpa os campos da imagem, depois de salvar | |
$("#addtag #submit").click(function(){ | |
$("#taxonomy_image_id").val(''); | |
$("#image").html(''); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Define o slug da taxonomia para poder ser aplicado em outros lugares | |
define("TAXONOMY_NAME", 'tipos'); | |
/* | |
* Cria uma função para adicionar um novo campo | |
*/ | |
function create_image_taxonomy(){ | |
//Chama a função media pra chamar ficar disponivel o upload | |
wp_enqueue_media(); | |
echo '<div class="form-field">'; | |
echo ' <label for="taxonomy_image">Imagem</label>'; | |
echo ' <div id="image"></div>'; | |
echo ' <input type="hidden" name="taxonomy_image_id" id="taxonomy_image_id" />'; | |
echo ' <button class="button add_taxonomy_image" type="button">Adicionar Imagem</button>'; | |
echo '</div>'; | |
} | |
/* | |
* Cria uma função para adicionar um novo campo na parte da edição da imagem | |
*/ | |
function edit_image_taxonomy($taxonomy) { | |
//Chama a função media pra chamar ficar disponivel o upload | |
wp_enqueue_media(); | |
//Pega o id da taxonomia atual | |
$term_id = $taxonomy->term_id; | |
//Resgata o campo salvo, no caso o id da taxonomia | |
$taxonomy_image_id = get_term_meta($term_id, 'taxonomy_image_id', true); | |
//define uma variavel | |
$image = ''; | |
//Verifica se tem algum valor | |
if($taxonomy_image_id) { | |
//regasta a url da imagem | |
$image = wp_get_attachment_image_url($taxonomy_image_id, 'thumbnail'); | |
} | |
echo '<tr class="form-field term-image">'; | |
echo '<th scope="row"><label for=imagem">Imagem</label></th>'; | |
echo '<td>'; | |
echo ' <div id="image">'; | |
if($image) { | |
echo '<img src="'.$image .'" width="100" />'; | |
echo '<br /><button type="button" class="button delete_taxonomy_image">Remover Imagem</button>'; | |
} | |
echo '</div>'; | |
//coloco o id da imagem no campo taxonomy_image_id | |
echo ' <input type="hidden" name="taxonomy_image_id" id="taxonomy_image_id" value="'.$taxonomy_image_id.'" /> | |
<button class="button add_taxonomy_image" type="button">Adicionar Imagem</button>'; | |
echo '</td>'; | |
echo '</tr>'; | |
} | |
/* | |
* Cria uma função para salvar o id da imagem | |
*/ | |
function save_image_taxonomy($term_id, $tt_id, $taxonomy){ | |
if($taxonomy != TAXONOMY_NAME) return; | |
if(isset($_POST['taxonomy_image_id'])) { | |
update_term_meta($term_id, 'taxonomy_image_id' , (int)$_POST['taxonomy_image_id'] ); | |
} else { | |
update_term_meta($term_id, 'taxonomy_image_id', null ); | |
} | |
} | |
/* | |
* Cria uma função para executar todas as funções anteriores | |
*/ | |
function start_custom_taxonomy(){ | |
add_action(TAXONOMY_NAME.'_add_form_fields', 'create_image_taxonomy'); | |
add_action(TAXONOMY_NAME.'_edit_form_fields', 'edit_image_taxonomy'); | |
add_action('create_term', 'save_image_taxonomy', 10, 3); | |
add_action('edit_term', 'save_image_taxonomy', 10, 3); | |
} | |
/*Executa as funções ao iniciar o admin*/ | |
add_action('admin_init', 'start_custom_taxonomy'); | |
/* | |
* Cria uma função para o javascript para clicar no botão e abre a opção de upload | |
*/ | |
function admin_add_scripts(){ | |
//O arquivo estaria no tema em : assets/js | |
wp_enqueue_script('admin', get_template_directory_uri().'/assets/js/admin.js', ['jquery'], false, true); | |
} | |
//Executa a função para adicionar o script | |
add_action( 'admin_enqueue_scripts', 'admin_add_scripts' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment