Fahrzeug
class Custom_Meta_Box3 {
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(
'wichtigste3',
__( 'Fahrzeugspezifische Infos', 'fahrzeughandel' ),
array( $this, 'render_metabox' ),
'fahrzeug_handel',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$cs_typensch = get_post_meta( $post->ID, 'cs_typensch', true );
$cs_fahrgestell = get_post_meta( $post->ID, 'cs_fahrgestell', true );
$cs_neup = get_post_meta( $post->ID, 'cs_neup', true );
// Set default values.
if( empty( $cs_typensch ) ) $cs_typensch = '';
if( empty( $cs_fahrgestell ) ) $cs_fahrgestell = '';
if( empty( $cs_neup ) ) $cs_neup = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="cs_typensch" class="cs_typensch_label">' . __( 'Typenscheinnr.', 'fahrzeughandel' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="cs_typensch" name="cs_typensch" class="cs_typensch_field" placeholder="' . esc_attr__( '', 'fahrzeughandel' ) . '" value="' . esc_attr( $cs_typensch ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="cs_fahrgestell" class="cs_fahrgestell_label">' . __( 'Fahrzgestellnr.', 'fahrzeughandel' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="cs_fahrgestell" name="cs_fahrgestell" class="cs_fahrgestell_field" placeholder="' . esc_attr__( '', 'fahrzeughandel' ) . '" value="' . esc_attr( $cs_fahrgestell ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="cs_neup" class="cs_neup_label">' . __( 'Neupreis', 'fahrzeughandel' ) . '</label></th>';
echo ' <td>';
echo ' <input type="number" id="cs_neup" name="cs_neup" class="cs_neup_field" placeholder="' . esc_attr__( '', 'fahrzeughandel' ) . '" value="' . esc_attr( $cs_neup ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$cs_new_typensch = isset( $_POST[ 'cs_typensch' ] ) ? sanitize_text_field( $_POST[ 'cs_typensch' ] ) : '';
$cs_new_fahrgestell = isset( $_POST[ 'cs_fahrgestell' ] ) ? sanitize_text_field( $_POST[ 'cs_fahrgestell' ] ) : '';
$cs_new_neup = isset( $_POST[ 'cs_neup' ] ) ? floatval( $_POST[ 'cs_neup' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'cs_typensch', $cs_new_typensch );
update_post_meta( $post_id, 'cs_fahrgestell', $cs_new_fahrgestell );
update_post_meta( $post_id, 'cs_neup', $cs_new_neup );
}
}
new Custom_Meta_Box3;