Meta box costi
class Custom_content_offers {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'custom_content_offers',
__( 'Contenuto', 'master-am' ),
array( $this, 'render_content_offers' ),
'offers',
'advanced',
'high'
);
}
public function render_content_offers( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'custom_content_offesrnonce_action', 'custom_content_offesrnonce' );
// Retrieve an existing value from the database.
$custom_content_offesrcontentitle = get_post_meta( $post->ID, 'custom_content_offesrcontentitle', true );
$custom_content_offesrcustom_content = get_post_meta( $post->ID, 'custom_content_offesrcustom_content', true );
$custom_content_offesricon = get_post_meta( $post->ID, 'custom_content_offesricon', true );
$custom_content_offesrimmagine_contenuto = get_post_meta( $post->ID, 'custom_content_offesrimmagine_contenuto', true );
$custom_content_offesrcosto_scontato = get_post_meta( $post->ID, 'custom_content_offesrcosto_scontato', true );
$custom_content_offesrcosto = get_post_meta( $post->ID, 'custom_content_offesrcosto', true );
// Set default values.
if( empty( $custom_content_offesrcontentitle ) ) $custom_content_offesrcontentitle = '';
if( empty( $custom_content_offesrcustom_content ) ) $custom_content_offesrcustom_content = '';
if( empty( $custom_content_offesricon ) ) $custom_content_offesricon = '';
if( empty( $custom_content_offesrimmagine_contenuto ) ) $custom_content_offesrimmagine_contenuto = '';
if( empty( $custom_content_offesrcosto_scontato ) ) $custom_content_offesrcosto_scontato = '';
if( empty( $custom_content_offesrcosto ) ) $custom_content_offesrcosto = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="custom_content_offesrcontentitle" class="custom_content_offesrcontentitle_label">' . __( 'Inserisci il titolo del contenuto', 'master-am' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_content_offesrcontentitle" name="custom_content_offesrcontentitle" class="custom_content_offesrcontentitle_field" placeholder="' . esc_attr__( '', 'master-am' ) . '" value="' . esc_attr( $custom_content_offesrcontentitle ) . '">';
echo ' <p class="description">' . __( 'Inserisci il titolo del contenuto', 'master-am' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_content_offesrcustom_content" class="custom_content_offesrcustom_content_label">' . __( 'Contenuto', 'master-am' ) . '</label></th>';
echo ' <td>';
wp_editor( $custom_content_offesrcustom_content, 'custom_content_offesrcustom_content', array( 'media_buttons' => true ) );
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_content_offesricon" class="custom_content_offesricon_label">' . __( 'Inserisci l'IURL dell'icona', 'master-am' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_content_offesricon" name="custom_content_offesricon" class="custom_content_offesricon_field" placeholder="' . esc_attr__( '', 'master-am' ) . '" value="' . esc_attr( $custom_content_offesricon ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_content_offesrimmagine_contenuto" class="custom_content_offesrimmagine_contenuto_label">' . __( 'Immagine del contenuto', 'master-am' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_content_offesrimmagine_contenuto" name="custom_content_offesrimmagine_contenuto" class="custom_content_offesrimmagine_contenuto_field" placeholder="' . esc_attr__( '', 'master-am' ) . '" value="' . esc_attr( $custom_content_offesrimmagine_contenuto ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_content_offesrcosto_scontato" class="custom_content_offesrcosto_scontato_label">' . __( 'Costo scontato', 'master-am' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_content_offesrcosto_scontato" name="custom_content_offesrcosto_scontato" class="custom_content_offesrcosto_scontato_field" placeholder="' . esc_attr__( '', 'master-am' ) . '" value="' . esc_attr( $custom_content_offesrcosto_scontato ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_content_offesrcosto" class="custom_content_offesrcosto_label">' . __( 'Costo', 'master-am' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="custom_content_offesrcosto" name="custom_content_offesrcosto" class="custom_content_offesrcosto_field" placeholder="' . esc_attr__( '', 'master-am' ) . '" value="' . esc_attr( $custom_content_offesrcosto ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['custom_content_offesrnonce'] ) ? $_POST['custom_content_offesrnonce'] : '';
$nonce_action = 'custom_content_offesrnonce_action';
// Check if a nonce is set.
if ( ! isset( $nonce_name ) )
return;
// Check if a nonce is valid.
if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
return;
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Sanitize user input.
$custom_content_offesrnew_contentitle = isset( $_POST[ 'custom_content_offesrcontentitle' ] ) ? sanitize_text_field( $_POST[ 'custom_content_offesrcontentitle' ] ) : '';
$custom_content_offesrnew_custom_content = isset( $_POST[ 'custom_content_offesrcustom_content' ] ) ? wp_kses_post( $_POST[ 'custom_content_offesrcustom_content' ] ) : '';
$custom_content_offesrnew_icon = isset( $_POST[ 'custom_content_offesricon' ] ) ? sanitize_text_field( $_POST[ 'custom_content_offesricon' ] ) : '';
$custom_content_offesrnew_immagine_contenuto = isset( $_POST[ 'custom_content_offesrimmagine_contenuto' ] ) ? sanitize_text_field( $_POST[ 'custom_content_offesrimmagine_contenuto' ] ) : '';
$custom_content_offesrnew_costo_scontato = isset( $_POST[ 'custom_content_offesrcosto_scontato' ] ) ? sanitize_text_field( $_POST[ 'custom_content_offesrcosto_scontato' ] ) : '';
$custom_content_offesrnew_costo = isset( $_POST[ 'custom_content_offesrcosto' ] ) ? sanitize_text_field( $_POST[ 'custom_content_offesrcosto' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_content_offesrcontentitle', $custom_content_offesrnew_contentitle );
update_post_meta( $post_id, 'custom_content_offesrcustom_content', $custom_content_offesrnew_custom_content );
update_post_meta( $post_id, 'custom_content_offesricon', $custom_content_offesrnew_icon );
update_post_meta( $post_id, 'custom_content_offesrimmagine_contenuto', $custom_content_offesrnew_immagine_contenuto );
update_post_meta( $post_id, 'custom_content_offesrcosto_scontato', $custom_content_offesrnew_costo_scontato );
update_post_meta( $post_id, 'custom_content_offesrcosto', $custom_content_offesrnew_costo );
}
}
new Custom_content_offers;