Skip to content
Advertisement

Add custom field inside a do_shortcode

I’m trying to add a contact form into a page template with contact form 7. However, I want the destination email to come in from a custom field I have created.

The code that pulls the custom field data in is this:

<?php the_field('website_address'); ?>

The shortcode which lets me select the destination email at template level is:

<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="xxxxxx@example.com"]'); ?>

So I need to add the website_address into the shortcode. I’ve tried the following:

<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="<?php the_field('website_address'); ?>"]'); ?>

But it has a PHP tag inside a PHP tag so it doesn’t work…

So I tried these:

<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="the_field('website_address')"]'); ?>

<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="get_field('website_address')"]'); ?>

<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email=".get_field('website_address')."]'); ?>

<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="'.get_field('website_address').'"]'); ?>

None of which are working correctly, some even break the page.

I’m struggling to find answer so was wondering if someone could enlighten me – I might even be thinking of it all in the wrong way! Thanks a lot for looking 🙂

EDIT

There’s a function I need to include which I have added to functions.php but it still isn’t helping…

add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );

function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
  $my_attr = 'destination-email';

  if ( isset( $atts[$my_attr] ) ) {
    $out[$my_attr] = $atts[$my_attr];
  }

  return $out;
}

It’s from the page: https://contactform7.com/getting-default-values-from-shortcode-attributes/

Advertisement

Answer

Ok, I found a way to do this as explained here: https://wordpress.org/support/topic/getting-emaip-recipient-from-shortcode/

  1. Add the shortcode with the destination email in the page or post where you want to show the form:

    [contact-form-7 id=”123″ title=”Contact Form” destination-email=”xxxxxx@example.com”]

  2. Add the following code to the theme functions.php or your custom plugin.

    add_filter( ‘shortcode_atts_wpcf7’, ‘custom_shortcode_atts_wpcf7_filter’, 10, 3 ); function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) { $my_attr = ‘destination-email’; if ( isset( $atts[$my_attr] ) ) { $out[$my_attr] = $atts[$my_attr]; } return $out;

} 3. Add this code to the Form Tab:

[email* destination-email default:shortcode_attr]

//If you want this field hidden you can use this code instead:

[hidden destination-email default:shortcode_attr]
  1. In cforms7 Mail Tab add the code [destination-email] in the “To” field.

If you used the hidden field it will show the “Invalid mailbox syntax” error, but in my case I can ignore it because it works.

If the To field with hidden field doesn’t work, you can add the id to the email shorcode:

[email* destination-email default:shortcode_attr id:hidefield]

And hide the field with css:

#hidefield {
    display: none;
}

Then I just used this:

<?php echo do_shortcode('[contact-form-7 id="123" title="Contact Form" destination-email="'.get_field('email_address').'"]'); ?>

to add in the email address from the custom post type 🙂

Thanks everyone who answered!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement