how to add meta option in post and pages.

step 1

function register() {

add_meta_box (
‘meta_options_metabox’,
‘my meta Options’,
‘meta_metabox’,
‘post’,
‘normal’,
‘high’
);

}

/* add_meta_box( “metabox id” , “metabox title” , “callback function of HTML for metabox” , “position of metabox” “part of page where edit screen section should be shown”,”priority”,”callback arguments” )

a)  metabox id  -> id can be anything,whatever you want.but it can be different for all metabox.

b)metabox title ->Title of meta box,that will display on box.

c)callback function -> function,in which HTML of metabox will be called.

d)position -> wherever you want to display metabox you can.you can display it in post,page and custom post type(for custom post type,just write the name of your custom post type).

e)context -> part of page where edit screen section should be shown.(normal,advanced,side…..Default :advanced)

f)callback agruments ->  Arguments to pass into your callback function. The callback will receive the $post object and whatever parameters are passed through this variable.Default :1   */

STEP 2

/* a) create function to call HTML */

function meta_metabox()

{

/* b)   get values of metabox. */

$custom_fields = get_post_meta($post->ID);

/* c)    Use $custom_fields = get_post_meta($post->ID); as value of text box. */

/*write HTML*/

<form id=”abc” method = “POST” action=”” >
<table>
<tr>
<td style=”width: 100%”>customer</td>
<td><input type=”text” size=”80″ name=”cus_name” id=”gsname” value=”<?php echo $custom_fields[‘cus_name’][0]; ?>” /></td>
</tr>
<tr>
<td style=”width: 100%”>E-mail</td>
<td><input type=”text” size=”80″ name=”cus_email” id=”Payment” value=”<?php echo $custom_fields[‘cus_email’][0]; ?>” /></td>
</tr></table></form>

}

STEP 3

/* a)  save metabox values with save function. */

function save()    /* function name can be anything  */

{

global $post;
/*  b) update_post_meta(“post id” , “textbox name” , “get value of text box”) -> function used to save values of metabox  */
update_post_meta($post->ID, “cus_name”, $_POST[“cus_name”]);
update_post_meta($post->ID, “cus_email”, $_POST[“cus_email”]);
update_post_meta($post->ID, “cus_address”, $_POST[“cus_address”]);

/*  c) $post->ID ->Id of current post

d) cus_name ->name of text box.

e) cus_address ->get value of text box */

}

STEP 4

/* a) apply add_meta_box hook to show metabox in admin panel.Without writing it,nothing will show in admin panel. */

add_action( ‘add_meta_boxes’,’register’);   /*  “register”  is function to hook */

/* b) apply save_post hook to save values */

add_action( ‘save_post’, ‘save’);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

contact us