Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

color metabox

class Meh_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(
			'meh_color',
			__( 'Accent Color', 'abraham' ),
			array( $this, 'render_color_metabox' ),
			'post',
			'advanced',
			'default'
		);

	}

	public function render_color_metabox( $post ) {

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

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

		// Set default values.
		if( empty( $meh_meh_color_pick ) ) $meh_meh_color_pick = '#FFFFFF';

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

		echo '	<tr>';
		echo '		<th><label for="meh_meh_color_pick" class="meh_meh_color_pick_label">' . __( 'Color', 'abraham' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="color" id="meh_meh_color_pick" name="meh_meh_color_pick" class="meh_meh_color_pick_field" placeholder="' . esc_attr__( '', 'abraham' ) . '" value="' . esc_attr__( $meh_meh_color_pick ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['meh_nonce'] ) ? $_POST['meh_nonce'] : '';
		$nonce_action = 'meh_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;

		// Sanitize user input.
		$meh_new_meh_color_pick = isset( $_POST[ 'meh_meh_color_pick' ] ) ? sanitize_text_field( $_POST[ 'meh_meh_color_pick' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'meh_meh_color_pick', $meh_new_meh_color_pick );

	}

}

new Meh_Meta_Box;