Add this code to your themes functions.php
function post_color_get_meta( $value ) {
global $post;
$field = get_post_meta( $post->ID, $value, true );
if ( ! empty( $field ) ) {
return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
} else {
return false;
}
}
function post_color_add_meta_box() {
add_meta_box(
'post_color-post-color',
__( 'post_color', 'post_color' ),
'post_color_html',
'post',//<- you can put custom post type name here//
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'post_color_add_meta_box' );
function post_color_html( $post) {
wp_nonce_field( '_post_color_nonce', 'post_color_nonce' ); ?>
<p>Change post background color</p>
<p>
<label for="post_color_select_color"><?php _e( 'select color', 'post_color' ); ?></label><br>
<input type="color" name="post_color_select_color" id="post_color_select_color" value="<?php echo post_color_get_meta( 'post_color_select_color' ); ?>">
</p><?php
}
function post_color_save( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! isset( $_POST['post_color_nonce'] ) || ! wp_verify_nonce( $_POST['post_color_nonce'], '_post_color_nonce' ) ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( isset( $_POST['post_color_select_color'] ) )
update_post_meta( $post_id, 'post_color_select_color', esc_attr( $_POST['post_color_select_color'] ) );
}
add_action( 'save_post', 'post_color_save' );
Add this where you want color:
<div style="background-color:<?php echo post_color_get_meta( 'post_color_select_color' ); ?>">
//content//
</div>