I'm a beginner and I have a problem with a part of the script that I found in a post. Practically when printing the checkboxes it does it outside the table. I can't bring the content of the foreach into the variable $input.
is there anyone who can help me and tell me how to change the foreach part of the checkboxes?
Thanks a lot
here is the screenshot
<?php
class custommetamasterMetabox {
private $screen = array(
'masters',
);
private $meta_fields = array(
array(
'label' => 'Abstract',
'id' => 'abstract',
'type' => 'wysiwyg',
),
array(
'label' => 'Download Brochure',
'id' => 'download-brochure',
'type' => 'textarea',
),
array(
'label' => 'Script contact form',
'id' => 'script_form',
'type' => 'textarea',
),
array(
'label' => 'Durata del master',
'id' => 'duratadelmaster_49527',
'type' => 'text',
),
array(
'label' => 'Area',
'id' => 'area_master',
'type' => 'select',
'options' => array(
'MBA',
'Marketing',
'Risorse umane',
'Turismo',
'Fashion & Luxury',
'Sport',
'Agribusiness',
'Sanità',
),
),
array(
'id' => 'formula',
'label' => 'Formula',
'type' => 'select',
'options' => array(
'Full Time',
'Part Time',
'Executive',
),
),
array(
'label' => 'Inizio del master',
'id' => 'iniziodelcorso_26128',
'type' => 'text',
),
array(
'label' => 'Lingua del master',
'id' => 'linguadelmaster_75355',
'type' => 'text',
),
//array(
// 'label' => 'Formula del master',
// 'id' => 'partecipanti_31742',
// 'type' => 'text',
//),
array(
'label' => 'Modalità',
'id' => 'modalita',
'type' => 'checkbox_group',
'desc' => '',
'options' => array (
'Roma' => array (
'label' => 'Roma',
'value' => 'Roma'
),
'On-line' => array (
'label' => 'On-line',
'value' => 'On-line'
)
),
),
array(
'label' => 'Posti liberi Roma',
'id' => 'pl_roma',
'type' => 'text',
),
array(
'label' => 'Posti liberi On-line',
'id' => 'pl_online',
'type' => 'text',
),
array(
'label' => 'Immatricolazioni (%)',
'id' => 'immatricolazioni',
'type' => 'text',
),
array(
'label' => 'Testo aggiuntivo',
'id' => 'testo_aggiuntivo',
'type' => 'wysiwyg',
),
array(
'label' => 'Menu interno',
'id' => 'menu_interno_',
'type' => 'text',
),
array(
'label' => 'Interviste correlate',
'id' => 'interviste_correlate',
'type' => 'text',
),
);
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_fields' ) );
}
public function add_meta_boxes() {
foreach ( $this->screen as $single_screen ) {
add_meta_box(
'masters',
__( 'Masters', 'textdomain' ),
array( $this, 'meta_box_callback' ),
$single_screen,
'advanced',
'default'
);
}
}
public function meta_box_callback( $post ) {
wp_nonce_field( 'custommetamasterMetabox_data', 'custommetamasterMetabox_nonce' );
$this->field_generator( $post );
}
public function field_generator( $post ) {
$output = '';
foreach ( $this->meta_fields as $meta_field ) {
$label = '<label for="' . $meta_field['id'] . '">' . $meta_field['label'] . '</label>';
$meta_value = get_post_meta( $post->ID, $meta_field['id'], true );
switch ( $meta_field['type'] ) {
case 'textarea':
$input = sprintf(
'<textarea class="large-text" id="%s" name="%s" rows="5">%s</textarea>',
$meta_field['id'],
$meta_field['id'],
$meta_value
);
break;
case 'wysiwyg':
ob_start();
wp_editor($meta_value, $meta_field['id']);
$input = ob_get_contents();
ob_end_clean();
break;
case 'select':
$input = sprintf(
'<select multiple id="%s" name="%s">',
$meta_field['id'],
$meta_field['id']
);
foreach ( $meta_field['options'] as $key => $value ) {
$field_value = !is_numeric( $key ) ? $key : $value;
$input .= sprintf(
'<option %s value="%s">%s</option>',
$meta_value === $field_value ? 'selected' : '',
$field_value,
$value
);
}
$input .= '</select>';
break;
case 'checkbox_group':
$input = sprintf(
'<div id="%s" name="%s">',
$meta_field['id'],
$meta_field['id']
);
foreach ( $meta_field['options'] as $option ) {
echo '<div id="'.$option['value'].'"><input type="checkbox" value="'.$option['value'].'" name="'.$meta_field['id'].'[]" id="'.$option['value'].'"', $meta_value && in_array($option['value'], $meta_value) ? ' checked="checked"' : '',' /><label for="'.$option['value'].'">'.$option['label'].'</label></div>';
}
$input .= '</div>';
break;
default:
$input = sprintf(
'<input %s id="%s" name="%s" type="%s" value="%s">',
$meta_field['type'] !== 'color' ? 'class="regular-text"' : '',
$meta_field['id'],
$meta_field['id'],
$meta_field['type'],
$meta_value
);
}
$output .= $this->row_format( $label, $input );
}
echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}
public function row_format( $label, $input ) {
return sprintf(
'<tr><th scope="row">%s</th><td>%s</td></tr>',
$label,
$input
);
}
public function save_fields( $post_id ) {
if ( ! isset( $_POST['custommetamasterMetabox_nonce'] ) )
return $post_id;
$nonce = $_POST['custommetamasterMetabox_nonce'];
if ( !wp_verify_nonce( $nonce, 'custommetamasterMetabox_data' ) )
return $post_id;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->meta_fields as $meta_field ) {
if ( isset( $_POST[ $meta_field['id'] ] ) ) {
switch ( $meta_field['type'] ) {
case 'email':
$_POST[ $meta_field['id'] ] = sanitize_email( $_POST[ $meta_field['id'] ] );
break;
case 'text':
$_POST[ $meta_field['id'] ] = sanitize_text_field( $_POST[ $meta_field['id'] ] );
break;
}
update_post_meta( $post_id, $meta_field['id'], $_POST[ $meta_field['id'] ] );
} else if ( $meta_field['type'] === 'checkbox' ) {
update_post_meta( $post_id, $meta_field['id'], '0' );
}
}
}
}
if (class_exists('custommetamasterMetabox')) {
new custommetamasterMetabox;
};
foreach
forcheckbox_group
you should append to$input
variable and you use,
instead of.
(twice) to concatenate strings. Before and after$meta_value && in_array($option['value'], $meta_value)
.