• Resolved FeralReason

    (@feralreason)


    I have landing pages with links to a Contact Form 7 form page. When a user clicks the link, I capture the landing page URL and write it to a cookie called “landingPage” (using an on-click event). What I want is add that landing page URL to the message body so my client knows which page the user clicked from. My preference is to add a special mail tag called ‘_landingpage’ that reads the cookie and inserts it in the email message body.

    I can hack the Contact Form 7 plugin to do this by adding the following code in the mail.php wpcf7_special_mail_tag() function:

    if ('_landingpage' == $name) {
          $cookie_name = "landingPage";
          if(!isset($_COOKIE[$cookie_name])) {
              return 'Original page not retrievable.';
          } else {
              return  $_COOKIE[$cookie_name];
          }
      }

    But, since this will be overwritten with a plugin update, I should create a function in functions.php (or a custom plugin) to do this. I thought the hook might be: ‘wpcf7_before_send_mail’. But I can’t seem to figure out how to add a mail tag — or even overwrite an existing mail tag with the cookie value to use that.

    // Failed attempt to overwrite the ‘_url’ mail tag value with my landingPage URL:

    function URL_of_form_link( $cf7 )
    {
      $cookie_name = "landingPage";
      if(!isset($_COOKIE[$cookie_name])) {
          $cf7->posted_data["_url"] = 'Original page not retrievable.';
      } else {
          $cf7->posted_data["_url"] = $_COOKIE[$cookie_name];
      }
      // ... no permutation I tried worked
    }
    add_action( 'wpcf7_before_send_mail', 'URL_of_form_link' );

    Does anyone have an idea of how to do this?

    https://www.ads-software.com/plugins/contact-form-7/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    The wpcf7_before_send_mail action hook isn’t appropriate for that purpose. Use the wpcf7_special_mail_tags filter instead.

    Thread Starter FeralReason

    (@feralreason)

    Takayuki — Thanks so much for your quick response!

    I’ll try it out tomorrow & post the results.

    Also — thanks for your continuing work on this great plugin!

    Thread Starter FeralReason

    (@feralreason)

    Takayuki, modeled this off of your ‘wpcf7_special_mail_tag’ function in mail.php, and added it to my site-specific plugin. Seems to work like a charm — and provides me with a way to continue adding special mail tags ??

    add_filter( 'wpcf7_special_mail_tags', 'custom_mail_tag', 10, 3 );
    
    function custom_mail_tag( $output, $name, $html ) {
    	$name = preg_replace( '/^wpcf7\./', '_', $name ); // for back-compat
    
    	$submission = WPCF7_Submission::get_instance();
    
    	if ( ! $submission ) {
    		return $output;
    	}
    
      if ('_landingpage' == $name) {
          $cookie_name = "landingPage";
          if(!isset($_COOKIE[$cookie_name])) {
              return 'Original page not retrievable.';
          } else {
              return  $_COOKIE[$cookie_name];
          }
      }
    
      return $output;
    }

    Let me know if anything looks like it could use improvement.

    Thanks again !!

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    That’s very good. It would be better if you escaped the cookie value like return esc_html( $_COOKIE[$cookie_name] ).

    Thread Starter FeralReason

    (@feralreason)

    Takayuki,

    You’re right – I should always escape on output.
    Thx so much for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add a Custom Mail Tag to Contact Form 7’ is closed to new replies.