Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add metabox WordPress

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 213
    Comment on it

    Welcome to Findnerd. In a series of wordPress functions, today i am going to discuss the wordPress function add_meta_box. WordPress is full on with customized options. Meta boxes is a wonderful option to customize. Meta-boxes are the special type of custom fields. They can be displayed on custom post type screen , page ,post,comment screen and on more. meta-boxes can contain the data of our choice. It can be simple text boxes, drop-down and more so we start with function's parameters to explain.

     

    Parameters Description
    id(string) It is a unique name to metabox. Useful to make design changes and more.
    title(string) Assign the title to metaboxes.
    callback(callback function) You can put the html using this function. ECHO the data to display.
    screen(string/array) This is optional one. Before version 4.4 it took only string and now it takes array to apply it on mutiple screen. Its value can be post,page and custom post type and more.
    Context(string) It implies that metabox position to display on the screen. Its value are different for different screens. comment screen uses 'Normal' and 'side' and post screen uses 'advanced' ,'normal' and 'side'. Default value is set to advanced.
    priority(string) it takes value low,default and high. Its default value is default and it is optional one.
    callback_args(array) this  is optional one and represents the second parameter of the callback function. default value is NULL.

     

    You can check the different parameters in the function in above table. Now we take an example to explain the process.

    function fn_extra_details(){
    	add_meta_box( 'fn_user_detail', 'Extra Details', 'extra_details_callback', 'post', 'normal', 'high' );
    	add_action( 'save_post', 'store_extra_details', 10, 2 );
    }
    add_action('add_meta_boxes', 'fn_extra_details');
    function extra_details_callback()
    {
    	echo '<input type="text" name="fn_extra_shot" id="fn_extra_shot" />';
    }
    function store_extra_details($post_id, $post)
    {
    	//save your metabox details
    }

     

    In above example we are using hook add_meta_boxes to add the metabox on post screen. We have set the other options such as priority to high,context set to normal. After setting up the metaboxes you need to save these details. You can use save_post hook to save the details. We have made the action for the same.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: