How to Add Custom Text Field in WordPress Without Plugin

How to Add Custom Text Field in WordPress Without Plugin

WordPress has given lot of flexibility to use custom field in post/page type by Adding Meta Box. So, i would like to talk about Meta Box to go through about the next of my article. Question may arise what is meta box and why we need this ? I can say easily, the meta-box is one kind of individual input field along with post type which allow to store that filed data to provide specific information. Suppose i may need one particular specific information ‘my hobby’ along with post type and this information i want to display in template with different stylish way. Then we can use meta box that will completely help to get out this situation.

We need a small introducing of the function which actually work for us to create a meta box in admin panel.

add_meta_box( $id, $title, $callback, $post_type, $context,$priority, $callback_args );

So now we add this function to admin for administrative interface. Here the example is shown for add “My Favorite Team” that will add to default post-type:

That’s all.  If you refresh your post type page, you can see this mata box added under the rich area. Now question how to get this value in template. There are two functions for this:

Function 1:  $meta_values = get_post_meta( $post_id, $key, $single );

Function 2: $custom_fields = get_post_custom($post->ID);

So if use first function, we can call it from inside the loop :

But if we use second function, we have to  get all meta values for this post id 1st then get the specific meta values:

So that’s all. Hope there is no confusion to get this meta value through meta boxes.Still if you have any further question feel free to drop a message here. We will response in our shortest period.

This post has already been read 3482 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

POSTED ON: Wordpress Basics, Wordpress Development

3 thoughts on “How to Add Custom Text Field in WordPress Without Plugin”

    1. Hi Damian,
      Its very simple. Just replace ‘post’ to your custom-post-type here :
      add_meta_box('my_fav_team_id', 'My Favorite Team', 'my_fav_team_field', 'your-custom-post-type', 'normal', 'high');

      That’s all 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *