【问题标题】:Wordpress get attribute from a shortcode within a pageWordpress 从页面中的简码获取属性
【发布时间】:2014-09-29 23:15:46
【问题描述】:

我正在构建一个 wordpress 联系表单插件,当使用简码时输出一个简单的联系表单。短代码有​​一个属性recipient_email,这是用户希望所有电子邮件从表单转到的电子邮件地址。我正在尝试在我的 php 邮件程序文件中获取收件人电子邮件的值。

这是我的简码输出的 html 及其属性:

extract(shortcode_atts(array(
            'el_class' => '',
            'title' => '', 
            'subtitle' => '',
            'recipient_email' => '',
        ), $atts));

        $el_class = $this->getExtraClass($el_class);
        $css_class = apply_filters(VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG,$el_class, $this->settings['base']);
        $subtitle = '<legend>'.$subtitle.'</legend>';           

        $output .= '<div class="contact-form '.$css_class.'" >';
        $output .= '<h4 class="form-title">'.$title.'</h4>';            

        $output .=  '<div id="contact">

        <div id="message"></div>

        <form method="post" action="'. VCPB_PLUGIN_URL . 'contact.php' .'" name="contactform" id="contactform">

        <fieldset>';

        $output .= $subtitle;

        $output .= '<label for="name" accesskey="U"><span class="required">*</span> Your Name</label>
        <input name="name" type="text" id="name" size="30" value="" />

        <br />
        <label for="email" accesskey="E"><span class="required">*</span> Email</label>
        <input name="email" type="text" id="email" size="30" value="" />

        <br />
        <label for="phone" accesskey="P"><span class="required">*</span> Phone</label>
        <input name="phone" type="text" id="phone" size="30" value="" />

        <br />
        <label for="comments" accesskey="C"><span class="required">*</span> Your message</label>
        <textarea name="comments" cols="40" rows="5" id="comments" style="width: 350px;"></textarea>

        <p><span class="required">*</span> Are you human?</p>

        <label for="verify" accesskey="V">&nbsp;&nbsp;&nbsp;3 + 1 =</label>
        <input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />

        <input type="submit" class="submit" id="submit" value="Submit" />';
        $output .= '</fieldset>

        </form>

</div>';          
        $output .= '</div>'; /* END .contact-form */
        return $output; 

如您所见,表单中的信息被发送到contact.php,后者处理表单并通过电子邮件发送出去。

这是我的 php 邮件文件 (contact.php) 的主要部分:

$address = "$recipient_email";
$e_subject = 'You\'ve been contacted by ' . $name . '.';
$e_body = "You have been contacted by $name, their message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email or via phone $phone";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

// Email has sent successfully, echo a success page.

echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";

} else {
echo 'ERROR!';
}

这里最重要的是我要解决的问题:

$address = "$recipient_email";

我希望 $recipient_email 成为输入短代码的值。谁能帮我弄清楚我会怎么做?

【问题讨论】:

  • 那么你创建了这个短代码吗?您用来从简码输出 html 的代码在哪里?
  • @manishie 刚刚将整个简码代码添加到我的问题中。谢谢

标签: php wordpress email shortcode


【解决方案1】:

有两种方法可以做到这一点 要么通过隐藏的输入类型发送收件人(这不是一个好的选择)

将表单操作放在同一页面上,而不是放在contact.php上 在同一页面上,您可以使用 shortcode_atts 获取收件人电子邮件

【讨论】:

  • 谢谢你,我想结合脚本是要走的路。如果我将它们组合起来,我应该将什么作为表单操作?
  • 不采取任何形式的行动。这会将表单提交到相同的 url。
猜你喜欢
  • 1970-01-01
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
  • 2020-04-20
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多