Post types came into existence in the 1.5 version of WordPress. A field ‘post_type’ got added into the database. However, it was not until version 2.9 that post type could be used in the admin panel. Now with the release of WordPress 3.0, a powerful and new feature called the custom post types has been introduced.
One another common and a useful feature of WP are the meta boxes that will enable you to add custom data to a page or post.
Meta Boxes are classified into three classes that you can add to the custom post type screen. These are:
- Built-in Metaboxes
- Taxonomy Metaboxes
- Custom Metaboxes
Built-in metaboxes as the name suggests are built-in to the custom post types. You can add them by calling a function, add_post_type_support. This can also be done by including these in the register_post_type function.
Taxonomy metaboxes are related to the taxonomy objects of WordPress. This can be explained by giving an example. post_tag and category. Both these objects have the metaboxes well defined that can be included in the custom post type interface.
Custom metaboxes are specifically created for the themes and the plugins. These metaboxes consist of HTML form elements that will enable the users to specify all the additional data and attributes to the custom post types.
Now let us have a look at the procedure of creating custom meta boxes in WordPress .
For adding meta box in a post type screen, add_meta_box function is used. This is then hooked to add_meta_boxes.
Below-mentioned code will add the metabox to post type edit screen.
function global_notice_meta_box() {
add_meta_box(
‘global-notice’,
_( ‘Global Notice’, ‘xyz’ ),
‘global_notice_meta_box_callback’,
‘post’
);
}
add_action( ‘add_meta_boxes’, ‘global_notice_meta_box’ );
For adding meta box in different post type screens like the page, post and book custom post, you will have to create the array of post types. Make use of the add_meta_box() for adding meta box.
function global_notice_meta_box() {
$screens = array( ‘post’, ‘page’, ‘book’ );
foreach ( $screens as $screen ) {
add_meta_box(
‘global-notice’,
__( ‘Global Notice’, ‘xyz’ ),
‘global_notice_meta_box_callback’,
$screen
);
}
}
add_action( ‘add_meta_boxes’, ‘global_notice_meta_box’ );
For adding meta box to the current post types make use of the get_post_types() for getting the array of post types. After this, interchange the value that $screen has in the above code with this.
function global_notice_meta_box() {
$screens = get_post_types();
foreach ( $screens as $screen ) {
add_meta_box(
‘global-notice’,
__( ‘Global Notice’, ‘xyz’ ),
‘global_notice_meta_box_callback’,
$screen
);
}
}
add_action( ‘add_meta_boxes’, ‘global_notice_meta_box’ );
You can even add meta box to the current and the new post types by not adding the third ($screen) argument.
function global_notice_meta_box() {
add_meta_box(
‘global-notice’,
__( ‘Global Notice’, ‘xyz’ ),
‘global_notice_meta_box_callback’
);
}
add_action( ‘add_meta_boxes’, ‘global_notice_meta_box’ );
You can even restrict meta box to a particular post by adding the name of the post type to add_meta_boxes by the following code.
function global_notice_meta_box() {
add_meta_box(
‘global-notice’,
__( ‘Global Notice’, ‘sitepoint’ ),
‘global_notice_meta_box_callback’
);
}
add_action( ‘add_meta_boxes_book’, ‘global_notice_meta_box’ );
Along with an array argument that is utilized by register_post_type for altering a post type there is a register_meta_box_cb function. The value is in the form of a certain callback function which is called while setting up meta boxes.
In The End
From the above-mentioned procedure, I hope that you will be able to successfully create create custom meta boxes in WordPress. You need to carefully follow the code and implement it in the same way as it has been stated above. I am sure that the whole process of adding meta boxes will become an easy one.
Auhtor Bio:
Alana Berge is working for Awebstar. A WordPress Website Development Company based in Singapore. She writes contents on WordPress, Web Design, Development, Internet, Social Networks, SEO, etc. Apart from this, she has helped a number of startups to grow their business online!!!