I’ve been using CMB2 for many years now. It’s a great way for creating custom metaboxes in WordPress for user profiles, post types, taxonomies and more.
The plugin as it stands has fields types that suit most situations. However, there are a few instances that I’ve come across where I’ve needed to add my own field types to properly manage the data that is being entered.
Most recently, I had a need to enter IP addresses and MAC address and have them properly sanitized. It was actually much simpler than I expected to add custom field types.
Here are the functions that I added for creating IP address fields and then a MAC address field:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Render the new field in the admin area. | |
* | |
* @param object $field The current CMB2_Field object. | |
* @param string $escaped_value The value of this field passed through the escaping filter. | |
* @param int $object_id The id of the object you are working with. | |
* @param string $object_type The type of object you are working with. | |
* @param object $field_type_object This is an instance of the CMB2_Types object. | |
* @return void | |
*/ | |
function cmb2_render_callback_for_ip_address( $field, $escaped_value, $object_id, $object_type, $field_type_object ) { | |
echo $field_type_object->input( array( 'type' => 'text' ) ); | |
} | |
add_action( 'cmb2_render_ip_address', 'cmb2_render_callback_for_ip_address', 10, 5 ); | |
/** | |
* Sanitize the input for the IP address field type | |
* | |
* @param null $override_value Sanitization override value to return. | |
* @param string $value The actual field value. | |
* @return string The sanitized IP address. | |
*/ | |
function cmb2_sanitize_ip_address_callback( $override_value, $value ) { | |
if ( ! filter_var( $value, FILTER_VALIDATE_IP ) ) { | |
$value = ''; | |
} | |
return $value; | |
} | |
add_filter( 'cmb2_sanitize_ip_address', 'cmb2_sanitize_ip_address_callback', 10, 2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Render the new field in the admin area. | |
* | |
* @param object $field The current CMB2_Field object. | |
* @param string $escaped_value The value of this field passed through the escaping filter. | |
* @param int $object_id The id of the object you are working with. | |
* @param string $object_type The type of object you are working with. | |
* @param object $field_type_object This is an instance of the CMB2_Types object. | |
* @return void | |
*/ | |
function cmb2_render_callback_for_mac_address( $field, $escaped_value, $object_id, $object_type, $field_type_object ) { | |
echo $field_type_object->input( array( 'type' => 'text' ) ); | |
} | |
add_action( 'cmb2_render_mac_address', 'cmb2_render_callback_for_mac_address', 10, 5 ); | |
/** | |
* Sanitize the input for the MAC address field type | |
* | |
* @param null $override_value Sanitization override value to return. | |
* @param string $value The actual field value. | |
* @return string The sanitized MAC address. | |
*/ | |
function cmb2_sanitize_mac_address_callback( $override_value, $value ) { | |
if ( ! filter_var( $value, FILTER_VALIDATE_MAC ) ) { | |
$value = ''; | |
} | |
return $value; | |
} | |
add_filter( 'cmb2_sanitize_mac_address', 'cmb2_sanitize_mac_address_callback', 10, 2 ); |
Like it? Share it!
- Click to share on Twitter (Opens in new window)
- Click to share on Facebook (Opens in new window)
- Click to share on LinkedIn (Opens in new window)
- Click to share on Pocket (Opens in new window)
- Click to share on Pinterest (Opens in new window)
- Click to share on Tumblr (Opens in new window)
- Click to share on Reddit (Opens in new window)
- Click to share on WhatsApp (Opens in new window)
- Click to share on Telegram (Opens in new window)
- Click to email a link to a friend (Opens in new window)
- Click to print (Opens in new window)