Add a Custom Mail Tag to Contact Form 7
-
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?
- The topic ‘Add a Custom Mail Tag to Contact Form 7’ is closed to new replies.