Adding metaboxes to post types

Metabox for custom post type

Metabox for custom post type

WordPress allows additional information to be input along with the posts. These additional information is entered through metaboxes. Metaboxes can be either simple textbox, listbox, or upload button or any other form element. For example a property description needs a contact details to be entered for each property listed. Creation of metaboxes has the following steps.

Create the custom post type

Custom post type can be set up using the register_post_type() function. This can be setup in your custom plugin file or in your functions.php in your custom theme. Following example shows how to setup a custom post type called Property

 

The above code registers a new post type called “Property” into the WordPress. The line to display metaboxes for custom post type is register_meta_box_cb. This callback function will call the ‘searchin_property_metaboxes’ to create metaboxes. You can also use add_meta_boxes action hook to register metaboxes too. This can be done in two ways. You can use this action hook for a specific post type by ‘add_meta_boxes-{post-type}’ action hook. Following are the ways to register meta boxes for an existing post type.

 Create metaboxes

The following code in callback function will register individual metaboxes for our custom post type.

Individual meta boxes are registered with add_meta_box() function. It takes following parameters

  • $id – The class id of the html element that displays metabox
  • $title – The title for metabox in the post edit screen.
  • $callback – Callback function to display metabox input element.
  • $screen – The type of writing screen where custom metabox should be created. For custom post types, just write the custom post type slug.
  • $context – The context in which the meta boxes are displayed. It could be either in side or normal or advanced
  • $priority – The priority in which the meta box is shown. It could be high, default or low.
  • $callback_args – Any arguments that can be passed to callback function.

Generate HTML for the registered meta box.

The HTML for the registered metabox can be created using the callback function. The callback function will output the metabox HTML. Following code will define how to create HTML for registered metabox.

This will display metabox when you are editing or creating a new post in your editing screen.

Saving the custom data in meta boxes

We need to use the action save_post to define a callback function which saves our custom data. The following example shows how to save data in custom metabox.

 

Display the metabox information in the template.

To display the metadata stored using metabox , we need to retrieve it using the post data. Later it can be displayed in the template. Following is a example to display metadata in a template file.

Metaboxes allow WordPress to be a CMS that can handle different kinds of data. Metaboxes really extend the functionality of WordPress to handle complex data. If you want to deal with lots of data, then the use of metaboxes is highly encouraged.

Leave a Reply