Here’s a quick code tip for Ultimate Member profiles. Let’s assume you want to display a phrase for your date field instead of display X years old or just displaying the date. This code snippet will allow you to display any phrase with the date for example; Have been married X years, Celebrating X years at the company and so much more. Sounds cool, right? Well let me show you how to get this done.
Step 1: Add the following code to your functions.php file or to a custom plugin.
<?php
/*
Plugin Name: Date to Phrase for Ultimate Member
Plugin URI: https://suiteplugins.com/
Description: Transform dates into phrases on user profiles.
Version: 1.0.0
Author: SuitePlugins
Author URI: https://suiteplugins.com/
*/
class UME_Date_Converter {
public $name = 'Date Converter';
public $slug = '';
public $description = 'Converts dates into phrases on profiles and directory.';
public function hooks() {
add_filter( 'um_core_fields_hook', [$this, 'um_core_fields_hook'], 10, 1 );
add_action( 'um_admin_field_edit_hook_ume_pretty_display', [$this,'um_admin_field_edit_hook'], 10, 1);
add_filter( 'um_profile_field_filter_hook__date', [$this, 'um_profile_field_filter_hook__date'], 101, 2 );
}
/**
* Add the new field to edit screen
*/
public function um_core_fields_hook( $fields = array() ) {
if ( ! empty( $fields['date'] ) ) {
// The item you want to add
$newItem = '_ume_pretty_display';
// Reference to the 'col2' array for easier manipulation
$col2 = $fields['date']['col2'];
// Find the position of '_pretty_format'
$position = array_search('_pretty_format', $col2);
// Check if '_pretty_format' was found in the array
if ($position !== false) {
// Increment position to insert after '_pretty_format'
$position++;
// Insert the new item into the 'col2' array at the specified position
array_splice($col2, $position, 0, $newItem);
}
$fields['date']['col2'] = $col2;
}
return $fields;
}
/**
* The html field on the edit screen.
*/
public function um_admin_field_edit_hook( $value = '', $form_id = '', $edit_array = array() ) {
$defaults = array(
'profile' => '',
);
$value = wp_parse_args( $value, $defaults );
?>
<p><label for="_ume_pretty_display"><?php esc_html_e( 'Formatted display on user profile', 'ultimate-member' ); ?> <?php UM()->tooltip( __( 'Shows the user date in a formatted message like Married for 25 years. I am 32 years old. Enter {value} for date in number format', 'ultimate-member' ) ); ?></label>
<textarea name="_ume_pretty_display[profile]"><?php echo wp_kses_post( $value['profile'] ); ?></textarea>
</p>
<?php
}
/**
* Formatting the display on the frontend.
*/
public function um_profile_field_filter_hook__date( $value, $data ) {
if ( ! $value ) {
return '';
}
// Ignore the format if pretty format is set because the date converts to X years old.
if ( ! empty( $data['pretty_format'] ) ) {
return $value;
}
// If the format is set then we'll convert the date to a number and display the message.
if ( ! empty( $data['ume_pretty_display']['profile'] ) ) {
$age = $this->get_age( $value );
return str_replace( '{value}', $age, $data['ume_pretty_display']['profile'] );
}
return $value;
}
/**
* Get date as number.
*
* @todo working with timestamps in this function.
*
* @param string $then
*
* @return string
*/
public function get_age( $then ) {
if ( ! $then ) {
return '';
}
$then_ts = strtotime( $then );
$then_year = date( 'Y', $then_ts );
$age = date( 'Y' ) - $then_year;
if ( strtotime( '+' . $age . ' years', $then_ts ) > current_time( 'timestamp' ) ) {
$age--;
}
if ( 0 === $age ) {
return 0;
}
// translators: %s: age.
return $age;
}
}
$class = new UME_Date_Converter();
$class->hooks();
Step 2: Go to your fields and set up a date field and you will find a new field setting labeled “Formatted display on user profile”. Here you will enter your phrase and use the placeholder {value} where the number of years will display.
Finally on the frontend your field will look like the following
Hope you find this useful for your Ultimate Member project. If you need help with your project, you can contact us as your Ultimate Member expert.