This is the structure that I use inside a plugin that I create when I have to use a metabox to assign a meta_key and a meta_value to a custom post type
<?php
add_action( 'add_meta_boxes', 'prefix_create_meta_box' );
function prefix_create_meta_box() {
add_meta_box(
'prefix_metabox_id', /*ID DELLA METABOX*/
'TITLE OF METABOX', /*TITOLO DELLA METABOX*/
'prefix_callback_meta_box', /*CALLBACK DELLA METABOX*/
'your_cpt', /*TIPO DI POST CHE VOGLIAMO A CUI VENGA APPLICATA LA METABOX*/
'side', /*POSIZIONE DELLA METABOX:*/
'default' ); /*PRIORITA' DELLA METABOX*/
}
function prefix_callback_meta_box($post) { /*CALLBACK DELLA METABOX*/
/* IMPLEMENTIAMO DELLE VARIABILI PER POTER MOSTRARE POI NEL FRONTEND IL RISULTATO DELLA NOSTRA METABOX
*******************************/
$prefix_metakey_metabox = get_post_meta( $post->ID, 'metakey_prefix_metabox', true );
/*------------------------------------------------------------------------------------------------------------*/
/* IMPLEMENTIAMO IL NONCE PER LA SICUREZZA DELLA NOSTRA METABOX
*******************************/
wp_nonce_field( plugin_basename( __FILE__ ), 'prefix_save_metabox' );
/* FINE IMPLEMENTAZIONE DEL NONCE PER LA NOSTRA METABOX
*******************************/
/*------------------------------------------------------------------------------------------------------------*/
?>
<input type="text" name="prefix_metavalue_metabox" id="prefix_metavalue_metabox" value="<?php echo $prefix_metakey_metabox; ?>" /></td>
<?php}
/*------------------------------------------------------------------------------------------------------------*/
/* HOOK PER SALVARE I NOSTRI DATI QUANDO VENGONO COMPILATI
*******************************/
add_action( 'save_post', 'prefix_save_metabox' );
function prefix_save_metabox( $post_id ) {
/* ELABORA I DATI SE $_POST è IMPOSTATO
*******************************/
if( isset( $_POST['prefix_metavalue_metabox'] ) )
{
/* SALVATAGGIO AUTOMATICO DEI DATI DELLA NOSTRA METABOX
*******************************/
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
/* CHECK DEL NONCE
*******************************/
wp_verify_nonce( plugin_basename( __FILE__ ), 'prefix_save_metabox' );
/* SALVATAGGIO DEI DATI E SANIFICAZIONE DEI CAMPI INPUT E SELECT
*******************************/
update_post_meta( $post_id, 'metakey_prefix_metabox', sanitize_text_field( $_POST['prefix_metavalue_metabox'] ) );
}
}
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->prefix}postmeta WHERE meta_value = '' ");
obviously these values end up in the post_meta table, I wanted to know how to save them in the termmeta table instead?
Test code:
add_action( 'add_meta_boxes', 'AMC_servizio_uno' );
function AMC_servizio_uno() {
add_meta_box(
'AMC_metabox_servizio_uno', /*ID DELLA METABOX*/
'Servizio uno', /*TITOLO DELLA METABOX*/
'plugin_add_custom_box', /*CALLBACK DELLA METABOX*/
'pro', /*TIPO DI POST CHE VOGLIAMO A CUI VENGA APPLICATA LA METABOX*/
'side', /*POSIZIONE DELLA METABOX:*/
'default' ); /*PRIORITA' DELLA METABOX*/
}
// Print the meta box
add_action( 'servizi_pro_edit_form', 'plugin_add_custom_box' );
function plugin_add_custom_box( $term ) {
$value = get_term_meta( $term->term_id, 'plugin_meta_key', true );
?>
<label for="plugin_meta_key">Description of the field: </label>
<input name="plugin_meta_key" type="text" value="<?php echo $value; ?>">
<?php
}
// Save the meta value
add_action( 'edit_term', 'plugin_save_term_meta' );
function plugin_save_term_meta( $term_id ) {
if ( array_key_exists( 'plugin_meta_key', $_POST ) ) {
update_term_meta(
$term_id,
'plugin_meta_key',
sanitize_text_field( $_POST['plugin_meta_key'] )
);
}
}
if I try to implement this structure it doesn't save me anything in the termmeta table
This is the structure that I use inside a plugin that I create when I have to use a metabox to assign a meta_key and a meta_value to a custom post type
<?php
add_action( 'add_meta_boxes', 'prefix_create_meta_box' );
function prefix_create_meta_box() {
add_meta_box(
'prefix_metabox_id', /*ID DELLA METABOX*/
'TITLE OF METABOX', /*TITOLO DELLA METABOX*/
'prefix_callback_meta_box', /*CALLBACK DELLA METABOX*/
'your_cpt', /*TIPO DI POST CHE VOGLIAMO A CUI VENGA APPLICATA LA METABOX*/
'side', /*POSIZIONE DELLA METABOX:*/
'default' ); /*PRIORITA' DELLA METABOX*/
}
function prefix_callback_meta_box($post) { /*CALLBACK DELLA METABOX*/
/* IMPLEMENTIAMO DELLE VARIABILI PER POTER MOSTRARE POI NEL FRONTEND IL RISULTATO DELLA NOSTRA METABOX
*******************************/
$prefix_metakey_metabox = get_post_meta( $post->ID, 'metakey_prefix_metabox', true );
/*------------------------------------------------------------------------------------------------------------*/
/* IMPLEMENTIAMO IL NONCE PER LA SICUREZZA DELLA NOSTRA METABOX
*******************************/
wp_nonce_field( plugin_basename( __FILE__ ), 'prefix_save_metabox' );
/* FINE IMPLEMENTAZIONE DEL NONCE PER LA NOSTRA METABOX
*******************************/
/*------------------------------------------------------------------------------------------------------------*/
?>
<input type="text" name="prefix_metavalue_metabox" id="prefix_metavalue_metabox" value="<?php echo $prefix_metakey_metabox; ?>" /></td>
<?php}
/*------------------------------------------------------------------------------------------------------------*/
/* HOOK PER SALVARE I NOSTRI DATI QUANDO VENGONO COMPILATI
*******************************/
add_action( 'save_post', 'prefix_save_metabox' );
function prefix_save_metabox( $post_id ) {
/* ELABORA I DATI SE $_POST è IMPOSTATO
*******************************/
if( isset( $_POST['prefix_metavalue_metabox'] ) )
{
/* SALVATAGGIO AUTOMATICO DEI DATI DELLA NOSTRA METABOX
*******************************/
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
/* CHECK DEL NONCE
*******************************/
wp_verify_nonce( plugin_basename( __FILE__ ), 'prefix_save_metabox' );
/* SALVATAGGIO DEI DATI E SANIFICAZIONE DEI CAMPI INPUT E SELECT
*******************************/
update_post_meta( $post_id, 'metakey_prefix_metabox', sanitize_text_field( $_POST['prefix_metavalue_metabox'] ) );
}
}
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->prefix}postmeta WHERE meta_value = '' ");
obviously these values end up in the post_meta table, I wanted to know how to save them in the termmeta table instead?
Test code:
add_action( 'add_meta_boxes', 'AMC_servizio_uno' );
function AMC_servizio_uno() {
add_meta_box(
'AMC_metabox_servizio_uno', /*ID DELLA METABOX*/
'Servizio uno', /*TITOLO DELLA METABOX*/
'plugin_add_custom_box', /*CALLBACK DELLA METABOX*/
'pro', /*TIPO DI POST CHE VOGLIAMO A CUI VENGA APPLICATA LA METABOX*/
'side', /*POSIZIONE DELLA METABOX:*/
'default' ); /*PRIORITA' DELLA METABOX*/
}
// Print the meta box
add_action( 'servizi_pro_edit_form', 'plugin_add_custom_box' );
function plugin_add_custom_box( $term ) {
$value = get_term_meta( $term->term_id, 'plugin_meta_key', true );
?>
<label for="plugin_meta_key">Description of the field: </label>
<input name="plugin_meta_key" type="text" value="<?php echo $value; ?>">
<?php
}
// Save the meta value
add_action( 'edit_term', 'plugin_save_term_meta' );
function plugin_save_term_meta( $term_id ) {
if ( array_key_exists( 'plugin_meta_key', $_POST ) ) {
update_term_meta(
$term_id,
'plugin_meta_key',
sanitize_text_field( $_POST['plugin_meta_key'] )
);
}
}
if I try to implement this structure it doesn't save me anything in the termmeta table
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.