Fields¶
Plugin is defining custom meta fields. All fields are implemented by using third party CMB2 plugin.
Fields definition are stored in includes/post-types/class-horizon-post-type-POST_TYPE_NAME.php
files. Each post type is using own fields()
static method for its fields definition.
Using predefined metaboxes¶
If you want to add built in metaboxes to a post type, you can use Horizon_Post_Types::add_metabox()
method (see example below).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | add_action( 'cmb2_init', 'doctor_fields' );
function doctor_fields() {
Horizon_Post_Types::add_metabox( 'doctor', array(
'general',
'banner',
'gallery',
'video',
'location',
'price',
'contact',
'flags',
'listing_category'
) );
}
|
Remove predefined metabox¶
If you want to remove built in metabox from a listing post type, you can use Horizon_Post_Types::remove_metabox()
method. In example below we removed “Video” metabox from “Doctor” listing type.
1 2 3 4 5 6 7 | add_action( 'cmb2_init', 'remove_metabox', 11 );
function remove_metabox() {
Horizon_Post_Types::remove_metabox( 'doctor', array(
'video',
) );
}
|
Create new field¶
It predefined metaboxes do’nt meet your requirements, you can create your own fields. Below is a small example how to add new “Fax” field into existing “Contact” metabox of “Doctor” listing type.
1 2 3 4 5 6 7 8 9 10 11 12 13 | add_action( 'cmb2_init', 'custom_fields', 11 );
function custom_fields( ) {
$contact_metabox = CMB2_Boxes::get( HORIZON_LISTING_PREFIX . 'doctor_contact' );
if ( ! empty( $contact_metabox ) ) {
$contact_metabox->add_field( array(
'id' => HORIZON_LISTING_PREFIX . 'doctor_fax',
'name' => __( 'Fax', 'domain' ),
'type' => 'text'
) );
}
}
|
Note
If you want to add field on the specific position, set second argument of add_field()
function.
Creating metabox¶
Adding new metaboxes is quite similar as extending them. We will define all metabox information instead of just adding new field. Below you can see an example how to define new “Details” metabox with “Salary” field for “Doctor” listing type. This is just a sample, for more information check CMB2 official documentation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | add_action( 'cmb2_init', 'custom_metabox' );
function custom_metabox() {
$details_metabox = new_cmb2_box( array(
'id' => HORIZON_LISTING_PREFIX . 'doctor_details',
'title' => __( 'Details', 'domain' ),
'object_types' => array( 'doctor' ),
'context' => 'normal',
'priority' => 'high',
'skip' => false
) );
$details_metabox->add_field( array(
'name' => __( 'Salary', 'domain' ),
'id' => HORIZON_LISTING_PREFIX . 'doctor_salary',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'min' => 0
)
) );
}
|
Note
All metabox fields are visible in listing detail attributes by default. To avoid that, set skip
field argument to true
.