Problem: I have a custom search View that shows a table of posts with filters. These posts include a repeating custom field where multiple email addresses can be stored. I would like to create a single link on the site that I can click to open a new email in Outlook, with all email addresses from these filtered posts used as recipients of the new email.
Solution:
If you want to send an email to each address in a multiple email address field, you can use a View to build a comma-separated list of addresses. Then you can use a special filter that strips out all the extra code from the View output, and use the resulting list of email addresses to build a dynamic "mailto" link. The final result will look something like this:
<a href="mailto:email1@gmail.com,email2@gmail.com,email3@gmail.com">Your link text</a>
Step 1. Create a new View that is a duplicate of the custom search View. In the Loop Output editor, replace the output with the code below:
[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[types field='email-field-slug' separator=',' output='raw'][/types],</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found][/wpv-no-items-found][wpv-layout-end]
Place this View somewhere on the same page as your custom search View. You should see a list of email addresses, separated by a comma. There may be some empty spaces, but that's okay.
Step 2. Set up a text filter that removes all the empty spaces and extra markup from the output. In your child theme's functions.php file, add this code:
function prefix_clean_view_output( $out, $id ) { $ids = array( 12345 ); if ( in_array( $id, $ids )) { $start = strpos( $out, '<!-- wpv-loop-start -->' ); if ( $start !== false && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false ) { $start = $start + strlen( '<!-- wpv-loop-start -->' ); $out = substr( $out , $start ); $end = strrpos( $out, '<!-- wpv-loop-end -->' ); $out = substr( $out, 0, $end ); } else { $start = strpos( $out, '>' ); if ( $start !== false) { $out = substr( $out, $start + 1 ); $end = strpos( $out, '<' ); $out = trim(substr( $out, 0, $end )); } } } return $out; } add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
Replace 12345 with the numeric ID of your new View. Now when you look at the results of this View you should not see the extra spaces or markup.
Step 3. Use the results of this View to create a mailto link:
<a href="mailto:[wpv-view name='your-new-view-slug']">Send an email</a>
Replace your-new-view-slug with the slug of your new View. Now replace the new View shortcode on your site with the link code above.
Relevant Documentation: https://toolset.com/documentation/user-guides/digging-into-view-outputs/
Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 12 replies, has 2 voices.
Last updated by culturaI 5 years, 2 months ago.
Assigned support staff: Christian Cox.
The forum ‘Types Community Support’ is closed to new topics and replies.