Custom meta field for custom post type in wordpress
In term of customization of WordPress theme development, some your theme need some extra field and meta box field for your custom post type.where you want to add some value that can be useful and important to your theme development.
Now its easy and short example that explain how add some extra field to POST, PAGE, CUSTOM POST TYPE page screen.
i am explaining step by step the whole code,
First of all go to your functions.php in your theme location. add the function and hooks into your functions.php file.
Register your metabox field fisrt.
1 2 3 4 5 |
add_action( 'admin_init', 'custom_meta_field_fnc' ); function custom_meta_field_fnc() { /* add_meta_box( $id, $title, $callback, $screen, $context,$priority, $callback_args ); */ add_meta_box( '_custom_meta_414', 'Custom Meta Title', 'display_my_new_field','post', 'normal', 'high' ); } |
Now your meta box is registered, its time to add your html with input field that will appear in your post, page or custom post type page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function display_my_new_field( $post ) { /*Retrive custom meta field value */ $fieldValue=esc_html(get_post_meta( $post->ID, '_custom_meta', true )); ?> <h4>Extra custom meta field</h4> <table width="100%"> <tr> <td style="width: 25%">Your Field Lable Name</td> <td><input type="text" name="_custom_meta" id="_custom_meta" value="<?php echo $fieldValue; ?>" /> </td> </tr> </table> <?php } |
Third step is to save your metabox value in database.
1 2 3 4 5 6 7 8 9 10 |
add_action( 'save_post', 'add_samplepost_fields', 10, 2 ); function add_samplepost_fields( $post_id, $post ) { /* Now saving custom meta field value */ if ( $post->post_type == 'post' ) { if(isset($_POST["_custom_meta"])) { update_post_meta($post_id,'_custom_meta',$_POST["_custom_meta"]); } } } |
Now every thing is ready to use. The whole code will be , you can copy and paste and edit the code as per your requirement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* ======================== add field ============================================================================*/ add_action( 'admin_init', 'custom_meta_field_fnc' ); function custom_meta_field_fnc() { /* add_meta_box( $id, $title, $callback, $screen= post,page or custom post type , $context,$priority, $callback_args ); */ /* add_meta_box( '_custom_meta_414', 'Custom Meta Title', 'display_my_new_field','post,page or custom post type', 'normal', 'high' );*/ add_meta_box( '_custom_meta_414', 'Custom Meta Title', 'display_my_new_field','post', 'normal', 'high' ); } function display_my_new_field( $post ) { /*Retrive custom meta field value */ $fieldValue=esc_html(get_post_meta( $post->ID, '_custom_meta', true )); ?> <h4>Extra custom meta field</h4> <table width="100%"> <tr> <td style="width: 25%">Your Field Lable Name</td> <td><input type="text" name="_custom_meta" id="_custom_meta" value="<?php echo $fieldValue; ?>" /> </td> </tr> </table> <?php } add_action( 'save_post', 'add_samplepost_fields', 10, 2 ); function add_samplepost_fields( $post_id, $post ) { /* Now saving custom meta field value */ /*( $post->post_type == 'post or page or your post type' ) */ if ( $post->post_type == 'post' ) { if(isset($_POST["_custom_meta"])) { update_post_meta($post_id,'_custom_meta',$_POST["_custom_meta"]); } } } /*=================================================== Add custom meta field in custom post type ===================*/ |
This is whole code. I hope its helpful to you.
When I originally left a comment I seem to have clicked the -Notify me when new comments are added- checkbox and now every time a comment is added I receive four emails with the same comment. Perhaps there is a way you are able to remove me from that service? Appreciate it!
It’s going to be finish of mine day, but before ending I am reading this wonderful post to increase my know-how.
I’m gone to convey my little brother, that he should also pay a quick visit this webpage on regular basis to take updated from most recent news update.
Hi there, every time i used to check blog posts here early in the dawn, for the reason that i love to find out more and more.
Hi, its fastidious article concerning media print, we all be familiar with media is a great source of information.
What’s up to every one, because I am in fact keen of reading this website’s post to be updated daily. It consists of fastidious stuff.
Good information. Lucky me I ran across your website by chance (stumbleupon). I have book marked it for later!
Greate article. Keep posting such kind of information on your page. Im really impressed by it.
Hello there, You have done an incredible job. I will definitely digg it and personally suggest to my friends. I’m confident they’ll be benefited from this website.
These are truly fantastic ideas in regarding blogging. You have touched some pleasant factors here. Any way keep up wrinting.
Hi terrific blog! Does running a blog similar to this require a massive amount work? I have virtually no knowledge of programming but I was hoping to start my own blog in the near future. Anyhow, should you have any ideas or techniques for new blog owners please share. I understand this is off topic however I just had to ask. Thanks a lot!
What’s up mates, how is the whole thing, and what you wish for to say about this piece of writing, in my view its genuinely amazing in support of me.
First off I want to say awesome blog! I had a quick question in which I’d like to ask if you do not mind. I was curious to know how you center yourself and clear your thoughts prior to writing. I have had trouble clearing my mind in getting my thoughts out. I do take pleasure in writing however it just seems like the first 10 to 15 minutes are wasted simply just trying to figure out how to begin. Any suggestions or tips? Thank you!
I got this website from my pal who told me concerning this web page and now this time I am browsing this web site and reading very informative articles at this time.
Great blog here! Also your web site loads up fast! What host are you using? Can I get your affiliate link to your host? I wish my website loaded up as quickly as yours lol
Great web site. Plenty of useful info here. I am sending it to some buddies ans also sharing in delicious. And of course, thanks to your sweat!
I know this if off topic but I’m looking into starting my own weblog and was curious what all is needed to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web savvy so I’m not 100% positive. Any tips or advice would be greatly appreciated. Thanks
Heya i’m for the primary time here. I found this board and I find It truly useful & it helped me out much. I hope to present one thing again and aid others like you helped me.
There’s definately a great deal to know about this topic. I really like all the points you made.
Hi there would you mind letting me know which hosting company you’re using? I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot quicker then most. Can you recommend a good internet hosting provider at a reasonable price? Kudos, I appreciate it!
Oh my goodness! Impressive article dude! Thank you, However I am going through problems with your RSS. I don’t know the reason why I am unable to subscribe to it. Is there anybody getting the same RSS problems? Anyone that knows the answer will you kindly respond? Thanx!!
Hey! Someone in my Facebook group shared this site with us so I came to look it over. I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers! Terrific blog and brilliant style and design.
You ought to take part in a contest for one of the best sites on the web. I am going to recommend this site!
certainly like your website but you need to check the spelling on several of your posts. Several of them are rife with spelling problems and I in finding it very troublesome to tell the reality then again I’ll certainly come back again.
With havin so much content and articles do you ever run into any issues of plagorism or copyright violation? My site has a lot of exclusive content I’ve either created myself or outsourced but it seems a lot of it is popping it up all over the web without my permission. Do you know any solutions to help reduce content from being stolen? I’d certainly appreciate it.
Your style is so unique compared to other folks I have read stuff from. Thanks for posting when you’ve got the opportunity, Guess I’ll just bookmark this site.
Hmm is anyone else encountering problems with the pictures on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any responses would be greatly appreciated.
You really make it seem so easy with your presentation but I find this topic to be really something that I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I’ll try to get the hang of it!
Hey! This is kind of off topic but I need some guidance from an established blog. Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about creating my own but I’m not sure where to begin. Do you have any points or suggestions? Appreciate it
If you want to improve your know-how only keep visiting this site and be updated with the most recent gossip posted here.
Normally I do not learn post on blogs, however I would like to say that this write-up very compelled me to check out and do so! Your writing taste has been surprised me. Thanks, very nice post.