【问题标题】:Using ACF get_field in custom form以自定义形式使用 ACF get_field
【发布时间】:2018-08-24 14:38:54
【问题描述】:

我正在使用 ACF 选项来保存联系表格的电子邮件地址。使用 Wordpress 多站点,我可以通过选项面板设置我希望表单发送给谁。表单正在发送自动回复,但未提交到保存的电子邮件地址。

<?php
$contact_form_email = get_field('contact_form_email','options');
$cyber_to = array($contact_form_email);
# CyberMark Subject Line
$cyber_subject  = $s." Form Submission";

# Headers of email
$cyber_headers  = "From: " . strip_tags('noreply@email.com') . "\r\n";
$cyber_headers .= "MIME-Version: 1.0" . "\r\n";
$cyber_headers .= "Content-Type: text/html; charset=ISO-8859-1" . "\r\n";

# CyberMark message
$cyber_msg = '<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Name:</td>
<td style="margin-left: 5px;">'.$n.'</td>
</tr>
<tr>
<td>Email:</td>
<td style="margin-left: 5px;">'.$e.'</td>
</tr>
<tr>
<td>Phone:</td>
<td style="margin-left: 5px;">'.$t.'</td>
</tr>
<tr>
<td>Message:</td>
<td style="margin-left: 5px;">'.$m.'</td>
</tr>
</table>';

# Loop through all email recipients
foreach($cyber_to as $cyber_email)
{
    mail($cyber_email,$cyber_subject,$cyber_msg,$cyber_headers);
}

?>

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    我从您的问题中猜想处理表单的 PHP 文件只是在 WP 文件夹中,它并没有真正在 wordpress“环境”中运行。

    您需要像这样在文件中实际加载 WP:

    require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
    

    这将加载所有的 wordpress 供您在该文件中使用,包括 get_field()。 ;)

    【讨论】:

    • 这行得通,表单处理!谢谢你。我现在的问题是它不会发送到 acf 选项区域中的电子邮件。我将代码更新为: $contactEmail = get_field('contact_email', 'options'); $cyber_to = $contactEmail;
    • 我从未使用过 ACF 的选项页面,但它似乎被保存为 wp_options 中的常规选项。在线快速帮助没有显示 get_field() 的任何用途。尝试在 WP 中获取选项的常规方式,get_option('your_option')。
    猜你喜欢
    • 2021-09-26
    • 2017-05-28
    • 2019-11-03
    • 2014-09-13
    • 2019-11-16
    • 2020-03-04
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多