0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
<?php
namespace WPTurbo;
function wpturbo_register_custom_taxonomy() {
register_taxonomy(
'tipo',
'post',
[
'label' => __( 'Tipo' ),
'rewrite' => [ 'slug' => 'tipo' ],
'hierarchical' => true,
]
);
}
add_action( 'init', 'WPTurbowpturbo_register_custom_taxonomy' );
function wpturbo_add_custom_field() {
?>
<div class="form-field">
<label for="tipo_imagen"><?php esc_html_e( 'Imagen', 'wpturbo' ); ?></label>
<input type="text" name="tipo_imagen" id="tipo_imagen" value="">
<button class="upload_image_button button"><?php esc_html_e( 'Seleccionar Imagen', 'wpturbo' ); ?></button>
</div>
<?php
}
add_action( 'tipo_add_form_fields', 'WPTurbowpturbo_add_custom_field' );
function wpturbo_edit_custom_field($term) {
$image = get_term_meta( $term->term_id, 'tipo_imagen', true ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="tipo_imagen"><?php esc_html_e( 'Imagen', 'wpturbo' ); ?></label></th>
<td>
<input type="text" name="tipo_imagen" id="tipo_imagen" value="<?php echo esc_attr( $image ); ?>">
<button class="upload_image_button button"><?php esc_html_e( 'Seleccionar Imagen', 'wpturbo' ); ?></button>
</td>
</tr>
<?php
}
add_action( 'tipo_edit_form_fields', 'WPTurbowpturbo_edit_custom_field' );
function wpturbo_save_custom_field($term_id) {
if ( isset( $_POST['tipo_imagen'] ) ) {
update_term_meta( $term_id, 'tipo_imagen', sanitize_text_field( $_POST['tipo_imagen'] ) );
}
}
add_action( 'created_tipo', 'WPTurbowpturbo_save_custom_field' );
add_action( 'edited_tipo', 'WPTurbowpturbo_save_custom_field' );
function wpturbo_enqueue_media_uploader() {
wp_enqueue_media();
?>
<script>
jQuery(document).ready(function($) {
var mediaUploader;
$('.upload_image_button').click(function(e) {
e.preventDefault();
if (mediaUploader) {
mediaUploader.open();
return;
}
mediaUploader = wp.media({
title: 'Seleccionar Imagen',
button: {
text: 'Seleccionar Imagen'
},
multiple: false
});
mediaUploader.on('select', function() {
var attachment = mediaUploader.state().get('selection').first().toJSON();
$('#tipo_imagen').val(attachment.url);
});
mediaUploader.open();
});
});
</script>
<?php
}
add_action( 'admin_enqueue_scripts', 'WPTurbowpturbo_enqueue_media_uploader' );
Con este código, podrás crear una categoría personalizada "Tipo" y asociar una imagen a cada término creado en esta taxonomía.