WordPress meta box is a very useful feature that allows you to add completely custom data to posts or pages in WordPress. Custom meta box contains data which allow admin to add/edit/delete post metadata. We can add any number of meta boxes to posts and pages on the WordPress admin side. Below is the best explanation for how to create post meta box in WordPress.
Custom meta box is the way where you can add an additional piece of data into your posts and pages. This data should be related to the main post. However, to understand how to custom meta boxes are useful for that you must understand how to add, update, delete and display post metadata.
For example, if you want to display testimonial listing and you want to add designation field with the testimonial post for that you need to add custom meta box for designation field.
Below are the functions for add, update, delete and display the meta box in WordPress.
- add_post_meta()
- update_post_meta()
- delete_post_meta()
- get_post_meta()
add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen screen = null, string $context = ‘advanced’, string $$priority = ‘default’, array $callback_args = null )
$id :- (string)(reuqired) ID for meta box.
$title :- (string)(reuqired) Meta box title.
$callback :- (callable)(reuqired) Funcion that fill the meta box with the content and the function should echo it’s output.
$screen :- (string|array)(optional) Screen in which to show meta box.
$context :- (string)(optional) The context within the screen where the box should be display.
$priority :- (string)(optional) The priority within the context where the box should show (‘low’ and ‘high’).
$callback_args :- (array)(optional) Data that should be set as $args property of the box array.
update_meta_box( $post_id, $meta_key, $meta_value, $prev_value )
$post_id :- (integer)(required) The ID of the post which you want to edit.
$meta_key :- (string)(required) The key of the custom field that you want to edit.
$meta_value :- (mixed)(required) The new value of the custom field.
$prev_value :- (mixed)(optional) The old value of the custom field that you want to change.
delete_meta_box( $post_id, $meta_key, $meta_value )
$post_id :- (integer)(required) The ID of the post which you want to delete.
$meta_key :- (string)(required) The key of the custom field that you want to delete.
$meta_value :- (mixed)(optional) The value of the field that you will delete.
get_meta_box( int $post_id, string $key = ‘ ‘, bool $single = false )
$post_id :- (integer)(required) The ID of the post.
$key :- (string)(optional) The meta key for retrive data.
$single :- (bool)(optional) Whether to return single value or not.
Below is the code for creating a custom meta boxes. For that, you need to add below code in a functions.php file of your current theme.
/** * Add custom metabox */ function Helpyouforever_add_metabox() { $screen = 'movies'; add_meta_box('demo_text','Extra Content','Helpyouforever_display_metabox', $screen, 'normal', 'high'); } add_action( 'add_meta_boxes', 'Helpyouforever_add_metabox' ) ; /** * Display PostMeta * @global type $wbdb * @param type $post */ function Helpyouforever_display_metabox($post) { global $wbdb; $sample_text = 'sample_text'; $get_display_text = get_post_meta( $post->ID,$sample_text, true ); ?> <label for="sample_text">Sample Text : </label> <input type="text" name="sample_text" id="sample_text" value="<?php echo $get_display_text;?>" /> <?php } /** * Save Post Meta * @param type $post */ function Helpyouforever_save_metabox($post) { $sample_text = $_POST['sample_text']; update_post_meta( $post, 'sample_text', $sample_text); } add_action('save_post','Helpyouforever_save_metabox');
After adding this code then you can see meta box option in your post as you given post type in the above code. below is the screenshot for meta box display in the post.
Hey there! This is my first visit to your blog! We are a team of volunteers and starting
a new project in a community in the same niche.
Your blog provided us valuable information to work on. You have done
a marvellous job!
But wanna input that you have a very nice internet site , I the design it actually stands out.
Thanks for your whole effort on this site. Debby loves working on research and it is easy to understand why. Almost all notice all concerning the dynamic method you provide useful ideas on this website and foster participation from visitors on that situation then my girl is really learning a great deal. Have fun with the rest of the year. You’re the one doing a powerful job.
Hello there,
My name is Aly and I would like to know if you would have any interest to have your website here at helpyouforever.com promoted as a resource on our blog alychidesign.com ?
We are in the midst of updating our broken link resources to include current and up to date resources for our readers. Our resource links are manually approved allowing us to mark a link as a do-follow link as well
.
If you may be interested please in being included as a resource on our blog, please let me know.
Thanks,
Aly
When someone writes an piece of writing he/she maintains
the plan of a user in his/her brain that
how a user can be aware of it. Therefore that’s why this paragraph is perfect.
Thanks!
This design is incredible! You most certainly know how to keep a reader entertained.
Between your wit and your videos, I was almost moved to start
my own blog (well, almost…HaHa!) Excellent job. I really loved what
you had to say, and more than that, how you presented it. Too
cool!
I got this site from my pal who informed me regarding this website and at
the moment this time I am browsing this site and reading very
informative content at this time.
What’s up to all, how is all, I think every one is getting more from this website,
and your views are fastidious for new people.
This site is my aspiration, rattling excellent style and design and perfect subject material.
Outstanding post, you have pointed out some good points, I besides conceive this s a very excellent website.
This is a very good tips especially to those new to blogosphere, brief and accurate information… Thanks for sharing this one. A must read article.
whoah this blog is fantastic i love reading your posts. Keep up the great work! You know, a lot of people are hunting around for this info, you can help them greatly.
Thanks for your interest.