Help Center

Showing User Age in the Listing Grid via Calculated Callback Add-on

Learn how to calculate the difference between years and display the user’s age or experience without forms and specific fields using the JetEngine’s Calculated callback add-on for WordPress.

Before you start, check the tutorial requirements:

This tutorial exemplifies a case of calculating and displaying the user-associated data on the front end without using forms and specific fields. In particular, here we use a code that adds a new callback to the Dynamic Field widget to display the user’s experience (in years) on the front end using a value inserted previously into a “Date” meta field.

Create a Meta Box for Users

First, move to the WordPress Dashboard > JetEngine > Meta Boxes directory. The “Add New” button at the top of the page allows one to add a new meta box.

Here, first fill out the Meta Box Title text field (we type “MB for Users”), next set the “User” Meta Box for, and move to the Visibility Conditions section to specify the “Edit User & Profile” option in the Visible at drop-down list.

adding a new meta box for users

After that, move to the Meta fields section and press the “New Meta Field” button. Here, create a “Date” meta field: fill out the Label text field (we type the “employment date”), set the “Date” Field Type, and enable the Save as timestamp toggle.

If needed, add other meta fields to the Meta Box.

adding meta fields to the meta box

After that, save the changes by pressing the “Create/Update Meta Box” button.

The How to Apply Meta Boxes to Users tutorial provides you with more details about configuring the user profile’s settings

In the last step, move to the WordPress Dashboard > Users > All Users tab and click the “Edit” button under the needed user or create a new one. Among the other default settings at the bottom of the page, you will see the meta fields you created previously. Fill in the fields with the appropriate information and click the “Update Profile” button.

filling out the user’s data

Download and Activate the Add-on

First, navigate to the Crocoblock Dev Tools page to observe all available add-ons.

the crocoblock devtools page

On this page, you can find the Calculated callback add-on, or click the JetEngine tab to download it there.

the calculate callback addon downloading

After that, install and activate the add-on as any WordPress plugin. Read more details in the How to Install Plugins Manually and How to Install JetPlugins via WordPress Dashboard tutorials.

Add the Code 

Next, head to the WordPress Dashboard > Appearance > Theme Editor and paste the code at the end of the Theme Functions file of your active WordPress theme.

Warning

The code presented herein is intended to calculate the difference between years; therefore, it can be used to specify age, experience, etc. In case you need to display users with birthdays this month on the current day and after, read the How to Generate SQL Query with Open AI: Displaying Users' Birthdays tutorial.

add_filter( 'jet-engine-calculated-callback/config', function( $callbacks = array() ) {
          $callbacks['DOB'] = function( $field_value ) {
                $dob = getdate($field_value);
        $today = getdate();
        $year_diff = $today['year'] - $dob['year'];
        if ($today['mon']>$dob['mon'] || ($today['mon']==$dob['mon'] && $today['mday']>=$dob['mday'])){
           return $year_diff;

        }


        return $year_diff-1;

  };


  return $callbacks;

} );
adding the code to the functionsphp file

In this code, one can edit the callback name by typing it instead of the ‘DOB’ text. 

Finally, once completed, press the “Update File” button.

Apply the Callback

Proceed to WordPress Dashboard > JetEngine > Listings/Components and click the “Add New” button. Select the “Users” option in the Listing source field. To create a “User Type” Listing, enter the specific name and choose the “Elementor” option in the Listing View field.

Then, add the Dynamic Field widget and configure the following settings: first, enable the Filter field output toggle; next, select the “Calculated field” Callback, and after that, choose the callback added via the code in the Calculated callbacks drop-down list (the ‘DOB’ callback, according to its name in the code).

In the last step, enable the Customize field output toggle and insert the needed text in the appeared Field format field.

the callback applied to display the user experience

Finally, publish the listing.
That’s it. Now you know how to calculate the difference between years and display the user’s age or experience without forms and specific fields using the JetEngine’s Calculated callback addon for WordPress.

Was this article helpful?
YesNo

Need help?

Ask Facebook community

Get answers from 30K+ Crocoblock Community experts from all over the world.

Start a live chat

Ask question to support agent and share its details via chat messages.