Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Staff Post Type

See how easy this was?

class Custom_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(
			'staff-details',
			__( 'Staff Details', 'fl-builder' ),
			array( $this, 'render_staff_metabox' ),
			'staff',
			'advanced',
			'default'
		);

	}

	public function render_staff_metabox( $post ) {

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

		// Retrieve an existing value from the database.
		$staff-name = get_post_meta( $post->ID, 'staff-name', true );
		$staff-title = get_post_meta( $post->ID, 'staff-title', true );
		$staff-email = get_post_meta( $post->ID, 'staff-email', true );
		$staff-department-head = get_post_meta( $post->ID, 'staff-department-head', true );

		// Set default values.
		if( empty( $staff-name ) ) $staff-name = '';
		if( empty( $staff-title ) ) $staff-title = '';
		if( empty( $staff-email ) ) $staff-email = '';
		if( empty( $staff-department-head ) ) $staff-department-head = '';

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

		echo '	<tr>';
		echo '		<th><label for="staff-name" class="staff-name_label">' . __( 'Name', 'fl-builder' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="staff-name" name="staff-name" class="staff-name_field" placeholder="' . esc_attr__( 'name', 'fl-builder' ) . '" value="' . esc_attr__( $staff-name ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="staff-title" class="staff-title_label">' . __( 'Title', 'fl-builder' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="staff-title" name="staff-title" class="staff-title_field" placeholder="' . esc_attr__( 'title', 'fl-builder' ) . '" value="' . esc_attr__( $staff-title ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="staff-email" class="staff-email_label">' . __( 'Email Address', 'fl-builder' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="email" id="staff-email" name="staff-email" class="staff-email_field" placeholder="' . esc_attr__( '[email protected]', 'fl-builder' ) . '" value="' . esc_attr__( $staff-email ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="staff-department-head" class="staff-department-head_label">' . __( 'Is this staff member a department head?', 'fl-builder' ) . '</label></th>';
		echo '		<td>';
		echo '			<label><input type="radio" name="staff-department-head" class="staff-department-head_field" value="0" ' . checked( $staff-department-head, '0', false ) . '> ' . __( 'No', 'fl-builder' ) . '</label><br>';
		echo '			<label><input type="radio" name="staff-department-head" class="staff-department-head_field" value="1" ' . checked( $staff-department-head, '1', false ) . '> ' . __( 'Yes', 'fl-builder' ) . '</label><br>';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['nonce'] ) ? $_POST['nonce'] : '';
		$nonce_action = '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.
		$new_staff-name = isset( $_POST[ 'staff-name' ] ) ? sanitize_text_field( $_POST[ 'staff-name' ] ) : '';
		$new_staff-title = isset( $_POST[ 'staff-title' ] ) ? sanitize_text_field( $_POST[ 'staff-title' ] ) : '';
		$new_staff-email = isset( $_POST[ 'staff-email' ] ) ? sanitize_email( $_POST[ 'staff-email' ] ) : '';
		$new_staff-department-head = isset( $_POST[ 'staff-department-head' ] ) ? $_POST[ 'staff-department-head' ] : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'staff-name', $new_staff-name );
		update_post_meta( $post_id, 'staff-title', $new_staff-title );
		update_post_meta( $post_id, 'staff-email', $new_staff-email );
		update_post_meta( $post_id, 'staff-department-head', $new_staff-department-head );

	}

}

new Custom_Meta_Box;