How to add meta box in WordPress?
Adding meta box is a very simple task to complete. It includes two hooks, one for displaying meta box in backend admin panel and other for saving meta fields in the database., and three functions to complete the process.
Firstly add function labo_client_meta_boxes(), this function includes add_meta_box() wordpress function. This function has 7 parmeters , from which first three are required parmeters.
function labo_client_meta_boxes( $post ) { add_meta_box( 'client_information', __( 'Client Information' ), 'render_client_information', 'post', 'normal', 'default' ); }
After the function is declared this needs to be hooked with add_meta_boxes_post hook of WordPress using add_action.
add_action( 'add_meta_boxes', 'labo_client_meta_boxes' );
Next thing we need to render the meta fields, render_client_information() function is callback parameter of add_meta_box.
function render_client_information($post) { global $post; $editor_id = 'labo_info_of_client'; $summary = get_post_meta( $post->ID,'labo_info_of_client', true ); wp_editor( $summary, $editor_id ); }
Finally, we need save input field values in the database, for doing this we need a create a function which will grab the metadata when a post is submitted and save that in the database, to accomplish this save_post action is used to complete the process.
function labo_save_wp_editor_fields($post_id) { if( isset( $_REQUEST['labo_info_of_client'] ) ){ update_post_meta( $post_id, 'labo_info_of_client',wp_kses_post( $_POST['labo_info_of_client'] ) ); } } add_action( 'save_post', 'labo_save_wp_editor_fields' );