What I have
WordPress site with ACF Pro and Advanced Forms addon. I have created a form to help my users unsubscribe from the newsletter.
User inputs his/her email and submits the form (ie. [email protected]
). Then, Advanced Forms handles the request and autoresponds with an email containing a generated link to the unsub script. The link looks like this: /[email protected]
.
What I want
Open text sucks, doesn't it? Providing a recipient with parameters in plain sight is an invitation to experiment. So I want to encode the email field value to Base64 as a basic deterrent.
I figured out the following flow:
+---------------------+
|User inputs email and|
|press SUBMIT |
+--------------+------+
|
|
v
+----------------+---------+
| Get "email" value and |
| encode to a new variable |
| "encodedEmail" |
+---------------+----------+
|
+--------------v-------------+
| Advanced Forms sends an |
| autoresponder to "email" |
| with a prepared link |
| containing "encodedEmail" |
+-------------+--------------+
|
v
+-------------+-------------+
| User clicks the link and |
| executes PHP that would |
| decode the address and do |
| its stuff afterwards. |
+---------------------------+
Am I right with this logic?
What I did
I was playing with af/email/before_send
and ACF's get_field()
with no success whatsoever.
Here's the code I put in WordPress
<?php
$args = array(
'submit_text' => 'Unsubscribe me',
);
advanced_form( 'MY_KEY_FORM', $args );
?>
I tried doing stuff like this:
function before_email_send() {
$emailOpenText = get_field( 'MY_FIELD' );
$email = base64_encode($emailOpenText);
update_field('ENCODED_EMAIL', $email);
}
Any ideas how to implement that properly? Thanks for your time. Any ideas appreciated.
AF documentation:
What I have
WordPress site with ACF Pro and Advanced Forms addon. I have created a form to help my users unsubscribe from the newsletter.
User inputs his/her email and submits the form (ie. [email protected]
). Then, Advanced Forms handles the request and autoresponds with an email containing a generated link to the unsub script. The link looks like this: https://example/[email protected]
.
What I want
Open text sucks, doesn't it? Providing a recipient with parameters in plain sight is an invitation to experiment. So I want to encode the email field value to Base64 as a basic deterrent.
I figured out the following flow:
+---------------------+
|User inputs email and|
|press SUBMIT |
+--------------+------+
|
|
v
+----------------+---------+
| Get "email" value and |
| encode to a new variable |
| "encodedEmail" |
+---------------+----------+
|
+--------------v-------------+
| Advanced Forms sends an |
| autoresponder to "email" |
| with a prepared link |
| containing "encodedEmail" |
+-------------+--------------+
|
v
+-------------+-------------+
| User clicks the link and |
| executes PHP that would |
| decode the address and do |
| its stuff afterwards. |
+---------------------------+
Am I right with this logic?
What I did
I was playing with af/email/before_send
and ACF's get_field()
with no success whatsoever.
Here's the code I put in WordPress
<?php
$args = array(
'submit_text' => 'Unsubscribe me',
);
advanced_form( 'MY_KEY_FORM', $args );
?>
I tried doing stuff like this:
function before_email_send() {
$emailOpenText = get_field( 'MY_FIELD' );
$email = base64_encode($emailOpenText);
update_field('ENCODED_EMAIL', $email);
}
Any ideas how to implement that properly? Thanks for your time. Any ideas appreciated.
AF documentation: https://advancedforms.github.io
Share Improve this question asked Dec 11, 2019 at 15:30 Lukasz PojezierskiLukasz Pojezierski 111 bronze badge 3 |1 Answer
Reset to default 0I believe you need the af/email/before_send
hook. I found this solution here: https://gist.github/mishterk/70bb486baac349c29b684346b77826ce
Rather than permanently modifying the email saved with the form entry, you can simply modify the message field to encode the email.
For example:
<?php
// Set the form key you wish to target
$form_key = 'form_5d97cf9edc0a8';
add_action( "af/email/before_send/key=$form_key", function ( $email, $form ) {
add_filter( 'wp_mail', function ( $data ) use ( $email ) {
// Take the "to" field and search for it in the message.
// If it exists, replace it with a base64 encoded version inside the message.
if($data['message'] && strpos($data['message'], $data['to'])){
$data['message'] = str_replace($data['to'], base64_encode($data['to']), $data['message']);
}
//Message before: Click on this link to unsubscribe <a href="example/[email protected]">Click Here</a>
//Message after: Click on this link to unsubscribe <a href="example/?email=dGVzdEB0ZXN0LmNvbQ==">Click Here</a>
return $data;
} );
}, 10, 2 );
Please note, this has not been tested.
- https://advancedforms.github.io/actions/af-email-before_send/
- https://gist.github/mishterk/70bb486baac349c29b684346b77826ce
before_email_send
in your functions.php of the theme, or elsewhere? The more context you can add, the better. – Tom Commented Dec 11, 2019 at 15:44