Change How Date Fields Display in User Insights Plugin

June 24, 2020 PHP, Plugins, WordPress

Change How Date Fields Display in User Insights Plugin

After adding a new custom field to the User Insights plugin recently, I wanted to change how the date fields were displaying. The default was just 20160403 in a YYYYMMDD format. I was pretty sure the client wanted a more legible display in a format they were used to.

Luckily, User Insights has provided a solution with a filter that can adjust the data before it’s printed to the screen. To quote the User Insights website, “The usin_user_db_data filter of Users Insights can be used to change how each value is represented in the user table and the export file.”

Here’s the code I came up with using this WordPress filter.

// Change Join Date display
function my_format_fieldname($data)
{
	if(!empty($data->usin_meta_date_joined))
	{
		$data->usin_meta_date_joined = date('F j, Y', strtotime($data->usin_meta_date_joined));
	}
	return $data;
}
add_filter('usin_user_db_data', 'my_format_fieldname');

My code inside the filter does a simple check to ensure the value exists, then updates the display to a more readable format. All date formats from the PHP date() function are accepted.

Since the Date Joined field is a custom field added through Advanced Custom Fields, the usin_meta_ portion is needed before the field name of date_joined. For non-custom default fields, only $data->fieldname is necessary without the “usin_meta_” prefix.

This was a very convenient solution that allows editing of any field value, including all custom fields added to User Insights.

Let's Talk About Your New Website

View Portfolio Contact Today