如何使高级表单(和/或ACF)对输入值进行编码?

时间:2019-12-11 作者:Lukasz Pojezierski

我拥有的WordPress网站,带有ACF Pro和高级表单插件。我创建了一个表单来帮助我的用户取消订阅新闻稿。用户输入其电子邮件并提交表单(即。[email protected]). 然后,高级表单处理请求,并自动回复一封电子邮件,其中包含生成的指向不明嫌犯脚本的链接。链接如下所示:https://example.com/[email protected].

我想要的是什么打开文本很糟糕,不是吗?向接收者提供清晰可见的参数是一种实验邀请。所以我想把email字段的值编码为Base64,作为一种基本的威慑。

我计算出以下流程:

  +---------------------+
  |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.     |
 +---------------------------+
我的逻辑正确吗?

我在玩什么af/email/before_send 和ACF的get_field() 没有任何成功。

这是我在WordPress中输入的代码

<?php 
     $args = array(
        \'submit_text\' => \'Unsubscribe me\',
         );
     advanced_form( \'MY_KEY_FORM\', $args ); 
?>
我试过这样做:

function before_email_send() {
   $emailOpenText = get_field( \'MY_FIELD\' );
   $email = base64_encode($emailOpenText);
   update_field(\'ENCODED_EMAIL\', $email);
   }
如何正确实施这一点,有什么想法吗?谢谢你的时间。欢迎提出任何想法。

AF documentation: https://advancedforms.github.io

1 个回复
SO网友:Tom

我相信你需要af/email/before_send 钩我在这里找到了这个解决方案:https://gist.github.com/mishterk/70bb486baac349c29b684346b77826ce

您可以简单地修改消息字段以对电子邮件进行编码,而不是永久性地修改使用表单条目保存的电子邮件。

例如:

<?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.com/[email protected]">Click Here</a>
        //Message after: Click on this link to unsubscribe <a href="example.com/?email=dGVzdEB0ZXN0LmNvbQ==">Click Here</a>

        return $data;

    } );

}, 10, 2 );


请注意,这还没有经过测试

相关推荐