How to Add Checkbox to page/post type in WordPress
In my previous discussion, we already made a clear conception of meta box and also learned how to add custom text field to any page/post type & how to add radio button to page/post.
Today we will discuss same thing but instead of text field we will show how to add checkbox for page/post type. The all functional process are same but need to changes the HTML format & added function for update the meta box.
Again we check the function which will 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. Let say we would like to get user input where user will select for whom the post is going to be published. To be developed we can create three input field with checkbox.
So here is the example:
/*********************************************************************/ /* Add featured post checkbox /********************************************************************/ add_action( 'add_meta_boxes', 'add_featured_checkbox_function' ); function add_featured_checkbox_function() { add_meta_box('featured_checkbox_id','FEATURED POST ?', 'featured_checkbox_callback_function', 'post', 'normal', 'high'); } function featured_checkbox_callback_function( $post ) { global $post; $isFeatured=get_post_meta( $post->ID, 'is_featured', true ); ?> <input type="checkbox" name="is_featured" value="yes" <?php echo (($isFeatured=='yes') ? 'checked="checked"': '');?>/> YES <?php } add_action('save_post', 'save_featured_post'); function save_featured_post($post_id){ update_post_meta( $post_id, 'is_featured', $_POST['is_featured']); }
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 :
$isFeatured=get_post_meta( $post->ID, 'is_featured', true ); echo $isFeatured;
But if we use second function, we have to get all meta values for this post id 1st then get the specific meta values:
$all_meta_fields= get_post_custom($post->ID); $isFeatured=$all_meta_fields["is_featured"][0]; echo $isFeatured;
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 21729 times!
This really answered my downside, thanks!
Good post but I was wondering if you could write a litte more on this topic? I’d be very grateful if you could elaborate a little bit more. Thanks!
Thanks @Mehedi Hasan, I did it!
but your function have some problem and I fixed it as follows:
/************************************************************************/
add_action(‘add_meta_boxes’,’add_featured_checkbox_function’ );
add_action(‘save_post’,’save_featured_post’);
function add_featured_checkbox_function() {
add_meta_box(‘featured_checkbox_id’,’FEATURED POST ?’, ‘featured_checkbox_callback_function’, ‘post’, ‘normal’, ‘high’);
}
function featured_checkbox_callback_function( $post ) {
$isFeatured=get_post_meta( $post->ID, ‘is_featured’, true );
?>
<input type="checkbox" name="is_featured" value="yes" /> YES
<?php
}
function save_featured_post($post_id){
update_post_meta( $post_id, 'is_featured', $_POST['is_featured']);
}
Thanks@huka 🙂
I missed the parameter to be passed in save function.
Just updated.
Cheers.
Thanks a lot!
I has find more site but not success, realy thanks wphats
[* Shield plugin marked this comment as “Pending Moderation”. Reason: Human SPAM filter found “searching” in “comment_content” *]
thanks man really helped me
have been searching for this