metadata generated from CMB2 disappears from DB
-
I use CMB2 to generate a set of custom metaboxes but sometimes (I can’t reproduce the condition that causes issue) those data are deleted from database.
When this occurs the data are deleted in every post, not only in the post being edited, sometimes the issue occurs even without any user logged.
I post my code:
<?php
//support function for CMB2 metaboxes
function AddFieldToMetabox($box, $prefix, $label, $name, $type, $repeatable = false, $previewSize = ”, $sanitize_callback = ”){
$args = array(
‘name’ => __( $label, ‘cmb2’ ),
‘id’ => ‘_esl_’ . $prefix . $name,
‘type’ => $type,
‘repeatable’ => $repeatable,
);if ( $previewSize != ”){
$args[‘preview_size’] = $previewSize;
}if ( $sanitize_callback != ”){
$args[‘sanitization_cb’] = $sanitize_callback;
} else {
$args[‘sanitization_cb’] = ‘sanitize_text_field’;
}$box->add_field( $args );
}add_filter( ‘cmb2_sanitize_gmap’, ‘cmb2_sanitize_map_callback’, 10, 2 );
add_action( ‘cmb2_render_gmap’, ‘sm_cmb_render_gmap’, 10, 5 );
add_filter( ‘cmb2_sanitize_smartslider’, ‘cmb2_sanitize_smartslider_callback’, 10, 2 );
add_action( ‘cmb2_render_smartslider’, ‘sm_cmb_render_smartslider’, 10, 5 );//removes iframe tag and google classes from Gmap
function cmb2_sanitize_map_callback( $override_value, $value ) {
// check if is gmaps iframe
if ( substr ( $value, 0 , 13 ) == ‘<iframe src=”‘) {
// remove iframe code
$value = substr ( $value, 13 , strlen($value) );
$value = substr ( $value, 0, strpos( $value, ‘”‘));
}
return $value;
}function sm_cmb_render_gmap( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
echo $field_type_object->input( array( ‘class’ => ‘regular-text’, ‘type’ => ‘text’ ) );
}// Metabox definition functions association to WordPress hook
add_action( ‘cmb2_admin_init’, ‘categorie_meta_box_taxonomy’ );
add_action( ‘cmb2_admin_init’, ‘add_metabox_contatti’ );
add_action( ‘cmb2_admin_init’, ‘add_metabox_galleria’ );
add_action( ‘cmb2_admin_init’, ‘add_metabox_mappa’ );
add_action( ‘cmb2_admin_init’, ‘add_metabox_slideshow’ );
add_action( ‘cmb2_admin_init’, ‘add_metabox_offerte’ );
add_action( ‘cmb2_admin_init’, ‘add_metabox_attrazione_home’ );function categorie_meta_box_taxonomy() {
$prefix = ‘_categoria’;$cmb_term = new_cmb2_box( array(
‘id’ => $prefix . ‘edit’,
‘title’ => __( ‘Immagine Categoria’, ‘cmb2’ ),
‘object_types’ => array( ‘term’ ),
‘taxonomies’ => array( ‘categoria’ )
) );AddFieldToMetabox($cmb_term, $prefix, ‘Immagine categoria’, ‘immagine’, ‘file’);
}function add_metabox_contatti() {
$prefix = ”;$cmb = new_cmb2_box(
array(
‘id’ => $prefix . ‘contatti’,
‘title’ => __( ‘Contatti’, ‘cmb2’ ),
‘object_types’ => array(‘hotel’, ‘residence’),
‘show_names’ => true
)
);$group_field_id = $cmb->add_field( array(
‘id’ => $prefix . ‘_field_indirizzo’,
‘type’ => ‘group’,
‘description’ => __( ‘Indirizzo’, ‘cmb2’ ),
‘repeatable’ => false,
‘options’ => array( ‘group_title’ => __( ‘Indirizzo’, ‘cmb2’ ) ),
) );// Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Via’,
‘id’ => ‘via’,
‘type’ => ‘text’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘CAP’,
‘id’ => ‘cap’,
‘type’ => ‘text’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Citta’,
‘id’ => ‘citta’,
‘type’ => ‘text’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Prov’,
‘id’ => ‘provincia’,
‘type’ => ‘text_small’
) );$group_field_id = $cmb->add_field( array(
‘id’ => $prefix . ‘_field_consigliato’,
‘type’ => ‘group’,
‘description’ => __( ‘Dati per mostrare la struttura prima delle altre nelle ricerche’, ‘cmb2’ ),
‘repeatable’ => false,
‘options’ => array( ‘group_title’ => __( ‘Consigliato’, ‘cmb2’ ) ),
) );// Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Consigliato’,
‘id’ => ‘consigliato’,
‘type’ => ‘checkbox’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Dal’,
‘id’ => ‘dal’,
‘type’ => ‘text_date’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Al’,
‘id’ => ‘al’,
‘type’ => ‘text_date’
) );$group_field_id = $cmb->add_field( array(
‘id’ => $prefix . ‘_field_contatti’,
‘type’ => ‘group’,
‘description’ => __( ‘Dati di contatto’, ‘cmb2’ ),
‘repeatable’ => false,
‘options’ => array( ‘group_title’ => __( ‘Contatti’, ‘cmb2’ ) ),
) );// Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Telefono’,
‘id’ => ‘telefono’,
‘type’ => ‘text_small’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Email’,
‘id’ => ’email’,
‘type’ => ‘text_email’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Dal’,
‘id’ => ‘dal’,
‘type’ => ‘text_date’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Al’,
‘id’ => ‘al’,
‘type’ => ‘text_date’
) );
}function add_metabox_offerte(){
$prefix = ”;$cmb = new_cmb2_box(
array(
‘id’ => $prefix . ‘offerte’,
‘title’ => __( ‘Offerte’, ‘cmb2’ ),
‘object_types’ => array(‘hotel’, ‘residence’),
‘show_names’ => true
)
);AddFieldToMetabox($cmb, $prefix, ‘Fascia di prezzo’, ‘fascia_prezzo’, ‘text’);
$group_field_id = $cmb->add_field( array(
‘id’ => $prefix . ‘_field_offerta’,
‘type’ => ‘group’,
‘description’ => __( ‘Offerte’, ‘cmb2’ ),
‘repeatable’ => true,
‘options’ => array(
‘group_title’ => __( ‘Offerta {#}’, ‘cmb2’ ),
‘add_button’ => __( ‘Aggiungi’, ‘cmb2’ ),
‘remove_button’ => __( ‘Rimuovi’, ‘cmb2’ )
),
) );// Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Titolo’,
‘id’ => ‘titolo’,
‘type’ => ‘text’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Descrizione’,
‘id’ => ‘descrizione’,
‘type’ => ‘text’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Prezzo (€)’,
‘id’ => ‘prezzo’,
‘type’ => ‘text_small’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Trattamento’,
‘id’ => ‘trattamento’,
‘type’ => ‘text’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Dal’,
‘id’ => ‘dal’,
‘type’ => ‘text_date’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Al’,
‘id’ => ‘al’,
‘type’ => ‘text_date’
) );$group_field_id = $cmb->add_field( array(
‘id’ => $prefix . ‘_field_lastminute’,
‘type’ => ‘group’,
‘description’ => __( ‘Last minute’, ‘cmb2’ ),
‘repeatable’ => true,
‘options’ => array(
‘group_title’ => __( ‘Last minute {#}’, ‘cmb2’ ),
‘add_button’ => __( ‘Aggiungi’, ‘cmb2’ ),
‘remove_button’ => __( ‘Rimuovi’, ‘cmb2’ )
),
) );// Id’s for group’s fields only need to be unique for the group. Prefix is not needed.
$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Titolo’,
‘id’ => ‘titolo’,
‘type’ => ‘text’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Descrizione’,
‘id’ => ‘descrizione’,
‘type’ => ‘text’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Prezzo (€)’,
‘id’ => ‘prezzo’,
‘type’ => ‘text_small’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Trattamento’,
‘id’ => ‘trattamento’,
‘type’ => ‘text’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Dal’,
‘id’ => ‘dal’,
‘type’ => ‘text_date’
) );$cmb->add_group_field( $group_field_id, array(
‘name’ => ‘Al’,
‘id’ => ‘al’,
‘type’ => ‘text_date’
) );
}function add_metabox_galleria() {
$prefix = ”;$cmb = new_cmb2_box(
array(
‘id’ => $prefix . ‘galleria’,
‘title’ => __( ‘Galleria’, ‘cmb2’ ),
‘object_types’ => array(‘hotel’, ‘residence’),
‘show_names’ => true
)
);AddFieldToMetabox($cmb, $prefix, ‘Galleria’, ‘galleria’, ‘file_list’, false, array( 150, 150 ));
}function add_metabox_slideshow(){
global $wpdb;$prefix = ‘_esl_’;
$cmb = new_cmb2_box(
array(
‘id’ => $prefix . ‘slideshow’,
‘title’ => __( ‘Slideshow’, ‘cmb2’ ),
‘object_types’ => array(‘hotel’, ‘residence’),
‘show_names’ => true
)
);//load slider from Smart Slider 3 table
$sliders = $wpdb->get_results( ‘SELECT * FROM hot_nextend2_smartslider3_sliders ORDER BY title’, ARRAY_N );$options = array();
if ( count( $sliders) > 0){
//slider found
for ( $i = 0; $i < count($sliders); $i++ ){
$options[$sliders[$i][0]] = $sliders[$i][1];
}
}//output Select
$cmb->add_field( array(
‘name’ => ‘slideshow’,
‘desc’ => ‘Seleziona uno slider’,
‘id’ => ‘_slideshow’,
‘type’ => ‘select’,
‘show_option_none’ => true,
‘default’ => ”,
‘options’ => $options,
) );}
function add_metabox_mappa(){
$prefix = ”;$cmb = new_cmb2_box(
array(
‘id’ => $prefix . ‘mappa’,
‘title’ => __( ‘Mappa’, ‘cmb2’ ),
‘object_types’ => array(‘hotel’, ‘residence’),
‘show_names’ => true
)
);AddFieldToMetabox($cmb, $prefix, ‘Mappa’, ‘mappa’, ‘gmap’, ”);
}function add_metabox_attrazione_home(){
$prefix = ”;$cmb = new_cmb2_box(
array(
‘id’ => $prefix . ‘in_home’,
‘title’ => __( ‘In home’, ‘cmb2’ ),
‘object_types’ => array(‘attrazioni’, ‘eventi’),
‘show_names’ => true
)
);AddFieldToMetabox($cmb, $prefix, ‘In home’, ‘in_home’, ‘checkbox’);
}?>
- The topic ‘metadata generated from CMB2 disappears from DB’ is closed to new replies.