Tell us what you are trying to do? I am using forminator pro that is a plugin from WPMUDEV and it sends email from the form to an admin email id but they are trying to help me send it to a custom field created in Toolset.. here's a link to support ticket.. versteckter Link .. and here's the page that I am trying to use it on.. versteckter Link
are we on the right track?
Is there any documentation that you are following?
Is there a similar example that we can see?
What is the link to your site?
Hello and thank you for contacting the Toolset support.
I assume you are talking about the code on this reply versteckter Link right?
I see possible errors:
- The value returned from types_render_field may be formatted, you may need to use the argument "output=raw" to get the raw value, check the documentation here https://toolset.com/documentation/customizing-sites-using-php/functions/
- The value returned is not an email and must not get added to "$recipients", you may need to use get_userdata($user_id)->user_email, check the documentation of this function here https://developer.wordpress.org/reference/functions/get_userdata/
- The value returned has probably multiple values separated by (,), you may need to explode it.
For the first point, you may also use the default WordPress function "get_post_meta" and prefixing the slug with "wpcf-", that's how Toolset stores the custom fields created by Toolset, check this article https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/
https://developer.wordpress.org/reference/functions/get_post_meta/
Check this sample code and please note that it needs testing:
add_filter( 'forminator_get_admin_email_recipients', function( $recipients ) {
global $post;
if ( function_exists( 'types_render_field' ) ) {
$custom_ids = get_post_meta( 'wpcf-notification-email', true );
// if multiple emails explode them
$custom_ids = explode(',', $custom_ids)
if ( count( $custom_emails ) > 0 ) {
unset( $recipients );
foreach ($custom_ids, $custom_id)
$recipients[] = get_userdata($custom_id)->user_email;
}
}
return $recipients;
} );
I hope this helps. Let me know what you will get.
Hi Jamal
Thanks for the update.. I have forwarded this to the WPMU guys and I'll ask them to test and decide..
Thanks once again.. will wait for their response and if they have some queries will get back to you.. if not, will close this ticket as resolved asap.
Have a nice day..:-)..
Regards,
Alim
Hi Jamal
Thanks for the update.. :-)..
Have forwarded the code to WPMU team and they confirmed it was all good..
But when I tested with multiple email ids it didn't seem to work.. I made the email custom field as 'Allow to add multiple instances' .. that's fine.. right? (screenshot attached)
Please do check the ticket at this URL to see if you can help the WPMU team
versteckter Link
Regards,
Alim
Hello Alim and thank you for your feedback. As the field stores emails instead of user IDs, we can omit the use of get_userdata/. I have also a mistake in the get_post_meta, it should take the post_id as the first argument and false as 3rd argument to return an array. We can use the $post for post_id, but I am not sure, maybe the WPMU team can help us get the ID of the created post.
The sample code will be:
add_filter( 'forminator_get_admin_email_recipients', function( $recipients ) {
global $post;
// get an array of emails from the custom field
$custom_emails = get_post_meta( $post->ID, 'wpcf-notification-email', false ); // <= change $post->ID with the ID of the created post.
if ( count( $custom_emails ) > 0 ) {
unset( $recipients );
foreach ($custom_emails as $custom_email) {
$recipients[] = $custom_email;
}
}
return $recipients;
} );
I'll remain at your disposal.
Hi Jamal
Thanks for the update..:-)..
The team at WPMU has used your suggestion and used this final code
add_filter(
'forminator_get_admin_email_recipients',
function( $recipients ) {
if ( function_exists( 'types_render_field' ) ) {
$custom_emails = get_post_meta( $_POST['page_id'], 'wpcf-optician-notification-email', false );
if ( count( $custom_emails ) > 0 ) {
unset( $recipients );
foreach ( $custom_emails as $recipient ) {
$recipients[] = trim( $recipient );
}
}
}
return $recipients;
}
);
And it seems to be working fine..:-)..
Please do check and confirm the above if it's all ok and I'll close the ticket
Regards,
Alim
Hello Alim,
The code looks good to me. But I'll need to test it to be sure. If you confirm that it is working for you, that's the test I'll need to do 🙂
Let me know if you still need help with anything.
Best regards,
Jamal