Skip Navigation

[Resolved] Create a “mailto” link with repeating custom field values

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

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

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/

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

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 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#604008

Hello my page is: hidden link

I would to setup a link from custom search to mail multiple recipients using outlook.

I have a mail repeater field (i can click over and i can send mail to a single recipient normally).

In VIEWS i can display multiple mail using the loop, but i would to use html mail-to tag to send to multiple recipients with a single click.

I can't seem to manage how to, could you to help me?

Thanks!

#604127

Hi, I'll try to help. If you want to send an email to each address in a multiple email address field, you can use the wpv-for-each shortcode in a loop to build comma-separated list of each address. 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="you@yoursite.com?bcc=user1@gmail.com,user2@gmail.com,user3@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:you@yourwebsite.com?bcc=[wpv-view name='your-new-view-slug']">Send an email BCC everyone</a>

Replace you@yourwebsite.com with whatever email address you use in the "from" field, and 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.

Let me know how it goes.

#604160

Hello Christian and thanks for your response.

I tried but i had no luck.

I duplicated my view and inserted in loop editor your code:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[types field='email' separator=',' output='raw'][/types],</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found][/wpv-no-items-found][wpv-layout-end]

I can display mail list correctly.

I also added your code to functions.php, and i can display again mail list correctly.

Then i created a text cell with the code

<p><a href="mailto:[wpv-view name='testmail']">Send</a></p>

I think in this step something is wrong, because my recipient become"<div style="

You can check on my page hidden link

In this way i also display 2 times [wpv-filter-meta-html] section (there are 2 views in the same page), i would simply display results like a table (like now on my page) and add link to send mass mail to filtered results.

#604468

I think in this step something is wrong, because my recipient become"<div style="
This indicates a problem in functions.php. Please check to make sure you replaced '12345' with the correct View ID, and make sure the functions.php file has been uploaded to your server or testing environment.

#604478

I done all the procedure again, double checked ID in place of 12345 and view-slug.

Recipient become: <form autocomplete=

#604527

Can I log in to your wp-admin area to see what's happening? Please provide login credentials here in the private reply fields.

#604529

Removed by admin

#604546

Okay please check now. I added this missing line to functions.php:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

Please note that you should not be modifying the Toolset Starter theme files directly. You should always use a Child Theme, because updating to a new version of the Toolset Starter theme will overwrite any changes you add to the parent theme's functions.php.

#604548

Yes you are right about child theme (however i edit functions.php via ftp saving to my local PC).

Thanks Christian, now i can send correctly mail to all contacts. But i would to display contacts as table (like i'm doing now) and send mail only to filtered contacts.

I'm not sure if with a single loop it's possible

#604566

That's correct, it's not possible to show both the table and the email link with a single View. Multiple Views that respond to the same URL parameters will be required.

#604570

Thanks Christian (also for your patience) It's possible to show 2 different views on one only parametric search?

#604709
Screen Shot 2018-01-09 at 7.18.37 AM.png

Yes, when you insert the second View shortcode you must select "only the search results" in the popup dialog (see screenshot). If both Views use the same filters and URL parameters, they will both update when the search form is submitted. The first View (the table and filters) must not use AJAX, and must update the page URL when the form is submitted. This way both Views can respond to the same filters.

#604833

Thanks Christian it worked perfectly as i wanted!

The only little problem is that even if i select "display only results" my filters are displayed anyway, however i deleted [wpv-filter-meta-html] section and now it work like a charm.

The forum ‘Types Community Support’ is closed to new topics and replies.

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