Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Meta Auriga

class boxen {

	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(
			'boxen',
			__( 'Info Boxen', 'boxen' ),
			array( $this, 'render_metabox' ),
			'boxen',
			'advanced',
			'high'
		);

	}

	public function render_metabox( $post ) {

		// Add nonce for security and authentication.
		wp_nonce_field( 'custom_nonce_action', 'custom_nonce' );

		// Retrieve an existing value from the database.
		$custom_link = get_post_meta( $post->ID, 'custom_link', true );
		$custom_titel = get_post_meta( $post->ID, 'custom_titel', true );
		$custom_beschrieb = get_post_meta( $post->ID, 'custom_beschrieb', true );

		// Set default values.
		if( empty( $custom_link ) ) $custom_link = '';
		if( empty( $custom_titel ) ) $custom_titel = '';
		if( empty( $custom_beschrieb ) ) $custom_beschrieb = '';

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

		echo '	<tr>';
		echo '		<th><label for="custom_link" class="custom_link_label">' . __( 'Link', 'boxen' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="url" id="custom_link" name="custom_link" class="custom_link_field" placeholder="' . esc_attr__( '', 'boxen' ) . '" value="' . esc_attr( $custom_link ) . '">';
		echo '			<p class="description">' . __( 'Linkziel eingeben', 'boxen' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="custom_titel" class="custom_titel_label">' . __( 'Titel', 'boxen' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="custom_titel" name="custom_titel" class="custom_titel_field" placeholder="' . esc_attr__( '', 'boxen' ) . '" value="' . esc_attr( $custom_titel ) . '">';
		echo '			<p class="description">' . __( 'Titel eingeben', 'boxen' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="custom_beschrieb" class="custom_beschrieb_label">' . __( 'Kurzbeschreibung', 'boxen' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="custom_beschrieb" name="custom_beschrieb" class="custom_beschrieb_field" placeholder="' . esc_attr__( '', 'boxen' ) . '" value="' . esc_attr( $custom_beschrieb ) . '">';
		echo '			<p class="description">' . __( 'Kurzbeschreibung eingeben', 'boxen' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['custom_nonce'] ) ? $_POST['custom_nonce'] : '';
		$nonce_action = 'custom_nonce_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 the user has permissions to save data.
		if ( ! current_user_can( 'edit_post', $post_id ) )
			return;

		// Check if it's not an autosave.
		if ( wp_is_post_autosave( $post_id ) )
			return;

		// Check if it's not a revision.
		if ( wp_is_post_revision( $post_id ) )
			return;

		// Sanitize user input.
		$custom_new_link = isset( $_POST[ 'custom_link' ] ) ? esc_url( $_POST[ 'custom_link' ] ) : '';
		$custom_new_titel = isset( $_POST[ 'custom_titel' ] ) ? sanitize_text_field( $_POST[ 'custom_titel' ] ) : '';
		$custom_new_beschrieb = isset( $_POST[ 'custom_beschrieb' ] ) ? sanitize_text_field( $_POST[ 'custom_beschrieb' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'custom_link', $custom_new_link );
		update_post_meta( $post_id, 'custom_titel', $custom_new_titel );
		update_post_meta( $post_id, 'custom_beschrieb', $custom_new_beschrieb );

	}

}

new boxen;