0

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

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 操作时是否不允许这样做?

4

1 回答 1

0

我发现我的函数在其他地方被调用(使用 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);

    }

}
于 2013-04-01T17:51:47.940 回答