PLUGINS_LOADED操作工作不正常

时间:2015-07-14 作者:Chaudhry Waqas

我试图在表单提交后向用户发送电子邮件,但出现错误
Call to undefined function wp_mail() in C:\\xampp\\htdocs\\wordpress\\wp-content\\plugins\\contact form\\contact-form-plugin.php on line 46<我在谷歌上搜索了一下,发现它与add_action( \'plugins_loaded\', \'functionShowForm\' );.我在代码中添加了这一行,但它在主窗体上方显示了另一个窗体,如下所示link<我做错了什么
我的插件文件的代码是

    <?php
    /*
    Plugin Name: Contact Form 
    Plugin URI: http://wpgeeks.net/
    Version: 1.0
    Author: 
    Description: 
    */

    /*Security Note: Consider blocking direct access to your plugin PHP files by adding the following line at the top of each of them, or be sure to refrain from executing sensitive standalone PHP code before calling any WordPress functions.*/
    defined( \'ABSPATH\' ) or die( \'No script kiddies please!\' );



    add_action( \'plugins_loaded\', \'functionShowForm\' );
    function functionShowForm($atts){

            $values = shortcode_atts(array(

        \'color\'=>\'white\'
    ),esc_html($atts));//updated here
    ?>

<form style="color:<?php echo $values[\'color\'];?>;" action ="#" method="post">
Name:   <input type="text" name="name" placeholder="First Name">
Email:  <input type="email" name="email" placeholder="[email protected]">
Password:<input type="password" name="password">
<input type="submit" name="submit" value="Submit">
</form>
<?php
}//function ends here

function process_wpse_194468(){
if ($_SERVER[\'REQUEST_METHOD\']=="POST" and isset($_POST["submit"])){
    $to = sanitize_email($_POST["email"]);//updated here
    $subject = esc_html($_POST["name"]);//updated here
    $message = $_POST["password"];
    echo "Email " .$to;
    wp_mail( $to, $subject, $message );
}
}
//short code
add_shortcode(\'showform\',\'functionShowForm\');
add_action(\'init\',\'process_wpse_194468\');

?>

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

plugins_loaded 将过早加载此表单,即使您没有收到错误。表单在打开之前加载<html 标签。您需要为此选择更好的挂钩--admin_notices 也许吧,但很难说清楚你到底想要什么。

其次,您需要分离处理程序:

function process_wpse_194468() {
  if ($_SERVER[\'REQUEST_METHOD\']=="POST" and isset($_POST["submit"])){
    $to = $_POST["email"];
    $subject = "Apple Computer";
    $message = $_POST["password"];
    echo "Email " .$to;
    wp_mail( $to, $subject, $message );
  }
}
add_action(\'init\',\'process_wpse_194468\');

Third, validate that input!

结束