最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to make Advanced Forms (andor ACF) encode input value?

programmeradmin3浏览0评论

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
  • Welcome! Can you post where your code is being fired? For example, do you have 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
  • Thanks @Tom. I have that code in specific page template. I am experimenting with functions and snippets you linked to. – Lukasz Pojezierski Commented Dec 12, 2019 at 16:53
  • Great! I would recommend the code I posted sitting inside Functions.php. Check out the explanation of what the function.php does and how it can help with these situations: developer.wordpress/themes/basics/theme-functions – Tom Commented Dec 13, 2019 at 15:10
Add a comment  | 

1 Answer 1

Reset to default 0

I 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
发布评论

评论列表(0)

  1. 暂无评论