Skip Navigation

[Resolved] How to show a user's information from user id in notification

This thread is resolved. Here is a description of the problem and solution.

Problem:

The user wanted the custom user field values from a user ID passed through the generic field in the form notification.

Solution:

Suggested to use the "cred_subject_notification_codes" and "cred_body_notification_codes" filters to register custom placeholders for form notifications.

Example:


add_filter('cred_subject_notification_codes', 'custom_generic_field_notification', 10, 1);
add_filter('cred_body_notification_codes', 'custom_generic_field_notification', 10, 1);
  
function custom_generic_field_notification( $defaultPlaceHolders ) {
    // get the user ID from the generic field 'evaluatee-user-id'
    $target_user_ID = $_REQUEST['evaluatee-user-id'];
    // get the first name and last name from the $target_user_ID
    $user_first_name = do_shortcode("[wpv-user field='user_firstname' id='".$target_user_ID."']");
    $user_last_name = do_shortcode("[wpv-user field='user_lastname' id='".$target_user_ID."']");
    // set those first name and last name values in the custom placeholders
    $newPlaceHolders = array( 
        '%%evaluatee_first_name%%' => $user_first_name,
        '%%evaluatee_last_name%%' => $user_last_name,
    );
     
    return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/forms/how-to-use-custom-placeholders-in-cred-notifications/

This support ticket is created 2 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by himanshuS 2 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#2224363

I am passing the user id through a generic field in the CRED form to send a notification to the user. That works just fine.

But, I want to extract the user first_name and last_name from the ID and add it to the notification.

I tired this

[wpv-user field='user_firstname' id='evaluatee-user-id'] [wpv-user field='user_lastname' id='evaluatee-user-id']

where evaluatee-user-id is the slug of the generic field but got nothing.

How can I activate the shortcode to show the data?

This user is not a current user so ID is required to show the data.

#2224561

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To show custom data in Toolset Form notifications, you can register custom placeholders, as explained in this guide:
https://toolset.com/documentation/programmer-reference/forms/how-to-use-custom-placeholders-in-cred-notifications/

In your function for the custom placeholder, you'll have the user ID in $_REQUEST['evaluatee-user-id'], which you can use to get any field value.

regards,
Waqar

#2224633

Thanks for this.

I did the following

add_filter('cred_subject_notification_codes', 'custom_generic_field_notification', 10, 1);
 
add_filter('cred_body_notification_codes', 'custom_generic_field_notification', 10, 1);
 
function custom_generic_field_notification( $defaultPlaceHolders ) {

$newPlaceHolders = array(
'%%message%%' => $_REQUEST['message'],
  '%%evaluatee-id%%' => $_REQUEST['evaluatee-user-id']
//'%%Email%%' => $_REQUEST['Email']
);
return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

Form setup here - hidden link

Value is shown in the form - hidden link

This still gives blank.

[wpv-user field='user_firstname' id=%%evaluatee-id%%] [wpv-user field='user_lastname' id=%%evaluatee-id%%]

What am I missing?

#2224911

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

The code for the custom placeholders will look like this:


add_filter('cred_subject_notification_codes', 'custom_generic_field_notification', 10, 1);
add_filter('cred_body_notification_codes', 'custom_generic_field_notification', 10, 1);
 
function custom_generic_field_notification( $defaultPlaceHolders ) {
	// get the user ID from the generic field 'evaluatee-user-id'
	$target_user_ID = $_REQUEST['evaluatee-user-id'];
	// get the first name and last name from the $target_user_ID
	$user_first_name = do_shortcode("[wpv-user field='user_firstname' id='".$target_user_ID."']");
	$user_last_name = do_shortcode("[wpv-user field='user_lastname' id='".$target_user_ID."']");
	// set those first name and last name values in the custom placeholders
	$newPlaceHolders = array( 
		'%%evaluatee_first_name%%' => $user_first_name,
		'%%evaluatee_last_name%%' => $user_last_name,
	);
	
	return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

After that, in your form notification body and subject line, you'll be able to use these new custom placeholders, directly like this:


Evaluatee First Name: %%evaluatee_first_name%%

Evaluatee Last Name: %%evaluatee_last_name%%

#2225015

The earlier solution also worked. I had to enable the snippet on AJAX calls.

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.