Creating fields in CMB2 for IP addresses and MAC addresses

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:

<?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 );
<?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 );

By Dave

Dave is the proud father of Ellie and Jack. There's nothing that makes him happier than spending time with his incredible wife and their amazing children. He's a civil/mechanical engineer and he also builds and maintains WordPress websites.

Leave a Reply