Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Event Metabox Final

class Sgr_Events_Meta_Box {

	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(
			'sgr_events_meta',
			__( 'Veranstaltungsdaten', 'sgr' ),
			array( $this, 'sgr_render_event_metabox' ),
			'veranstaltung',
			'advanced',
			'high'
		);

	}

	public function sgr_render_event_metabox( $post ) {

		// Retrieve an existing value from the database.
		$sgr_event_location = get_post_meta( $post->ID, 'sgr_event_location', true );
		$sgr_event_date = get_post_meta( $post->ID, 'sgr_event_date', true );
		$sgr_event_time = get_post_meta( $post->ID, 'sgr_event_time', true );
		$sgr_event_contributors = get_post_meta( $post->ID, 'sgr_event_contributors', true );
		$sgr_event_registration = get_post_meta( $post->ID, 'sgr_event_registration', true );

		// Set default values.
		if( empty( $sgr_event_location ) ) $sgr_event_location = '';
		if( empty( $sgr_event_date ) ) $sgr_event_date = '';
		if( empty( $sgr_event_time ) ) $sgr_event_time = '';
		if( empty( $sgr_event_contributors ) ) $sgr_event_contributors = '';
		if( empty( $sgr_event_registration ) ) $sgr_event_registration = '';

		// Form fields.
		echo '<table class="form-table">';

		echo '	<tr>';
		echo '		<th><label for="sgr_event_location" class="sgr_event_location_label">' . __( 'Veranstaltungsort', 'sgr' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="sgr_event_location" name="sgr_event_location" class="sgr_event_location_field" placeholder="' . esc_attr__( '', 'sgr' ) . '" value="' . esc_attr( $sgr_event_location ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="sgr_event_date" class="sgr_event_date_label">' . __( 'Veranstaltungsdatum', 'sgr' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="date" id="sgr_event_date" name="sgr_event_date" class="sgr_event_date_field" placeholder="' . esc_attr__( '', 'sgr' ) . '" value="' . esc_attr( $sgr_event_date ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="sgr_event_time" class="sgr_event_time_label">' . __( 'Veranstaltungsbeginn', 'sgr' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="time" id="sgr_event_time" name="sgr_event_time" class="sgr_event_time_field" placeholder="' . esc_attr__( '', 'sgr' ) . '" value="' . esc_attr( $sgr_event_time ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="sgr_event_contributors" class="sgr_event_contributors_label">' . __( 'Mitwirkende', 'sgr' ) . '</label></th>';
		echo '		<td>';
		echo '			<textarea id="sgr_event_contributors" name="sgr_event_contributors" class="sgr_event_contributors_field" placeholder="' . esc_attr__( '', 'sgr' ) . '">' . $sgr_event_contributors . '</textarea>';
		echo '			<p class="description">' . __( 'Leitung, Referenten, Kommentatoren, Special Guest', 'sgr' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="sgr_event_registration" class="sgr_event_registration_label">' . __( 'Anmeldung', 'sgr' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="email" id="sgr_event_registration" name="sgr_event_registration" class="sgr_event_registration_field" placeholder="' . esc_attr__( '', 'sgr' ) . '" value="' . esc_attr( $sgr_event_registration ) . '">';
		echo '			<p class="description">' . __( 'E-Mail-Adresse für Anmeldung', 'sgr' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Sanitize user input.
		$sgr_new_event_location = isset( $_POST[ 'sgr_event_location' ] ) ? sanitize_text_field( $_POST[ 'sgr_event_location' ] ) : '';
		$sgr_new_event_date = isset( $_POST[ 'sgr_event_date' ] ) ? sanitize_text_field( $_POST[ 'sgr_event_date' ] ) : '';
		$sgr_new_event_time = isset( $_POST[ 'sgr_event_time' ] ) ? sanitize_text_field( $_POST[ 'sgr_event_time' ] ) : '';
		$sgr_new_event_contributors = isset( $_POST[ 'sgr_event_contributors' ] ) ? sanitize_text_field( $_POST[ 'sgr_event_contributors' ] ) : '';
		$sgr_new_event_registration = isset( $_POST[ 'sgr_event_registration' ] ) ? sanitize_email( $_POST[ 'sgr_event_registration' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'sgr_event_location', $sgr_new_event_location );
		update_post_meta( $post_id, 'sgr_event_date', $sgr_new_event_date );
		update_post_meta( $post_id, 'sgr_event_time', $sgr_new_event_time );
		update_post_meta( $post_id, 'sgr_event_contributors', $sgr_new_event_contributors );
		update_post_meta( $post_id, 'sgr_event_registration', $sgr_new_event_registration );

	}

}

new Sgr_Events_Meta_Box;