繁体   English   中英

Wordpress-函数参数未传入

[英]Wordpress - Function parameters not passing in

我正在尝试编写一个在发布新帖子后向用户发送电子邮件的功能。 这是我到目前为止的内容:

require_once('db.php');

$sql="SELECT email FROM table";

$result = sqlsrv_query($sqlsrvconnection, $sql);

$emailList="";

while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
    $emailList.=", ".$row['email'];
}

add_action('save_post', 'email_members');

function email_members( $post_id, $emailList )  {

    if ( !wp_is_post_revision( $post_id ) ) {

        $to      = 'example@test.com';
        $subject = 'the subject';
        $message = "email list consists of: ".$emailList;
        $headers = 'From: webmaster@example.com' . "\r\n" .
            'Reply-To: webmaster@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);

}

}

在我的函数中,我尝试传递$ emailList变量,但无法执行任何操作。 我把它传递错了吗? 挂接到另一个wp动作时是否不允许这样做?

我发现我的函数在其他地方被调用(使用save_post动作)。 因此,该函数无权访问$ emailList变量。 我将所有内容(除了add_action())都移到了函数中,并且一切正常!

add_action('save_post', 'email_members');

function email_members( $post_id, $emailList )  {

    require_once('db.php');

    $sql ="SELECT email FROM table";

    $result = sqlsrv_query($sqlsrvconnection, $sql);

    $emailList="";

    while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
        $emailList.=", ".$row['email'];
    }

    if ( !wp_is_post_revision( $post_id ) ) {

        $to      = 'example@test.com';
        $subject = 'the subject';
        $message = "email list consists of: ".$emailList;
        $headers = 'From: webmaster@example.com' . "\r\n" .
            'Reply-To: webmaster@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM