WordPress Contact form 7 set recipient from custom shortcode

I can’t get this to work. And it seems impossible to get any real documentation of the process.

I have a custom shortcode in my functions.php, which gets an email adress from an external API. I would like to use this shortcode in the “TO” (i.e. recipient) field in WPCF7. But it keeps giving me an error warning of “syntax error”.

Read More

I need to find a way to make the recipient field accept a custom shortcode from my functions.php file.

I’ve tried the dynamic text plugin but it doesn’t work.

Here is an example of my shortcode

function single_email() {
    global $contact;
    return $contact->email;
}add_shortcode( 'contact_email', 'single_email' );

Maybe something along the line of using “wpcf7_special_mail_tags” and “do_shortcode()”? Not really sure how that works though.

Using Version 4.4.2 of WPCF7.

Related posts

Leave a Reply

2 comments

  1. Can you please check below code?

    wpcf7_add_shortcode('contact_email', 'single_email', true);
    function single_email(){
        global $contact;
        return $contact->email;
    }
    
  2. You can use like this create new field in wp_options table and use like this.

    On Your Contact page

          <div class="contant">
          <div class="section">
            <div class="contact_pannel">
              <h1>You can find us literally anywhere, just push a button and we’re there</h1>
              <div class="lf_pannel">
              <form action="" method="" id='myForm'  onsubmit=" return pretendValidation();  ">
                <h2>Use this form to send us a message:</h2>
                <div class="fild">
                    <label class="label">Name</label>
                    <input name="Name"   type="text" class="input">
                </div>
    
                <div class="fild">
                    <label class="label">Email</label>
                    <input name="Email" type="email" class="input" required=required  >
                </div>
    
                <div class="fild">
                    <label class="label">Company</label>
                    <input name="Company" type="text" class="input">
                </div>
    
                <div class="fild">
                    <label class="label">Subject</label>
                    <input name="Subject" type="text" class="input">
                </div>
    
                <div class="fild">
                    <label class="label">Message</label>
                    <textarea class="input" style="height:90px;" id='Message'></textarea>
                </div>
                    <input name="submitt" type="submit" id="submitt" style="display:none;">
    
                <a href="javascript:void(0);" class="send_button margin_right23" onclick="document.getElementById('mailed').style.display = 'none'; document.getElementById('submitt').click();" >Send Message</a>
              </form>
    
              <div class="  wpcf7-display-none " id="mailed"   role="alert">Your message was sent successfully. Thanks.</div>
              </div>
    
              <div class="rg_pannel">
    
    
              <?php echo html_entity_decode(  get_option('admin_address') );?>
    
    
    
    
    
              </div> 
            </div> 
          </div>
        </div>
    
        <script>
        function pretendValidation(){
    
    
        jQuery(document).ready(function($) {
    
            // We'll pass this variable to the PHP function example_ajax_request
            var fruit = 'Banana';
              // console.log($( "#myForm" ).serialize());
            // This does the ajax request
    
            $.post( "<?=get_template_directory_uri () ;?>/contact_ajax.php",
            { myForm: $( "#myForm" ).serialize(), Message: $( "#Message" ).val() },
            function( data ) {
              console.log( data ); // John
    
            });
    
    
    
    
             $('#myForm')[0].reset();
    
             $( "#mailed" ).show( "slow" );
    
        });
            return false;
        }
        </script>
    

    After that you have to create ajax page like this

    contact_ajax.php

    <?php
    
    /** Load WordPress Bootstrap */
    require_once  ( '../../../wp-load.php' );
    
    // echo "1232".site_url();
    
    
    // print_r($_REQUEST);
    
    $params = array();
    parse_str($_REQUEST['myForm'], $params);
    $params['Message']= $_REQUEST['Message'];
    // print_r($params);
    
    $to      = get_option('your_email_field_name');
    // $to      = "demo@youremail.in";
    $subject = 'Fokus Tech Contact Form';
      $message = "Hello,n"
                ."Name: ".$params ['Name']."n"
                ."Email: ". $params ['Email']."n"
                ."Company: ".$params ['Company']."n"
                ."Subject: ". $params ['Subject']."n"
                ."Message: ".$params['Message'] ."n"
    
    ;
    $headers = 'From: no-repaly@you.com' . "rn" .
        'Reply-To: no-repaly@you.com' . "rn" .
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    
    ?>