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:
// =============Add Meta Box My Favorite Team with Post type======== add_action('admin_init', 'add_my_fav_team' ); function add_my_fav_team() { add_meta_box('my_fav_team_id', 'My Favorite Team', 'my_fav_team_field', 'post', 'normal', 'high'); } function my_fav_team_field(){ global $post; $get_all_meta_values = get_post_custom($post->ID); $fav_team=$get_all_meta_values["fav_team"][0]; echo '<label>Favorite Team Name:</label> <input type="text" name="fav_team" size="100" value=" '.$fav_team.'" />'; } add_action('save_post', 'save_my_fav_team'); function save_link(){ global $post; update_post_meta($post->ID, "fav_team", $_POST["fav_team"]); }
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 :
$my_fav_team = get_post_meta($post->ID, 'fav_team', true); echo $my_fav_team;
But if we use second function, we have to get all meta values for this post id 1st then get the specific meta values:
$get_all_meta_values = get_post_custom($post->ID); $my_fav_team=$get_all_meta_values["fav_team"][0]; echo $my_fav_team;
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.
Thanks.
This post has already been read 7216 times!
I have custom post type. How i can add this for custom post?
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 🙂
Cheers.
it would be much easy, if you could add an image for the output of the above code.