Meta Box

Meta Box:

The add_meta_box() function was introduced in Version 2.5. It allows plugin developers to add meta boxes to the administrative interface.

This function should be called from the 'add_meta_boxes' action. This action was introduced in Version 3.0; in prior versions, use 'admin_init' instead.

———————– example codes—————————————–

<?php

//add a metabox in wp admin
add_action(‘add_meta_boxes’,’add_banner_metabox_for_page’);
// Save your meta box content
add_action( ‘save_post’, ‘save_banner_metabox_for_page’ );

function  add_banner_metabox_for_page(){
add_meta_box(
‘banner_shortcode_metabox’, // ID, should be a string
‘Banner Short Code Settings’, // Meta Box Title
‘banner_meta_box_content’, // Your call back function, this is where your form field will go
‘page’, // The post type you want this to edit screen section (‘post’, ‘page’, ‘dashboard’, ‘link’, ‘attachment’ or ‘custom_post_type’ where custom_post_type is the custom post type slug)
‘normal’, // The placement of your meta box, can be ‘normal’, ‘advanced’or side
‘high’ // The priority in which this will be displayed
);
}
function banner_meta_box_content($post){
//wp_nonce_field( basename( __FILE__ ), ‘prfx_nonce’ );
// Get post meta value using the key from our save function in the second paramater.
$banner_custom_meta = get_post_meta($post->ID, ‘_banner_custom_meta’, true);
?>
<table >
<tbody>
<tr>
<th scope=”row”>Short Code:</th>
<td><input type=”text” name=”bannermetabox_shortcode” id=”bannermetabox_shortcode” value=”<?php if(isset($banner_custom_meta)) echo $banner_custom_meta;?>” /></td>
</tr>
</tbody>
</table>
<?php
}
function save_banner_metabox_for_page(){
global $post;
// Get our form field
if( $_POST ) :

$banner_custom_meta = esc_attr( $_POST[‘bannermetabox_shortcode’] );

// Update post meta
update_post_meta($post->ID, ‘_banner_custom_meta’, $banner_custom_meta);

endif;
}

—————————————————————————————————————————–

Custom Post Meta Boxes:

No Comments

Post a Comment