[英]Wordpress - Function parameters not passing in
I am trying to write a function that emails my users after I publish a new post. 我正在尝试编写一个在发布新帖子后向用户发送电子邮件的功能。 Here is what I have so far:
这是我到目前为止的内容:
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);
}
}
In my function I have tried to pass in the $emailList variable, but I am not able to do anything with it. 在我的函数中,我尝试传递$ emailList变量,但无法执行任何操作。 Am I passing it in wrong?
我把它传递错了吗? Is this not allowed when hooking into another wp action?
挂接到另一个wp动作时是否不允许这样做?
I figured out that my function is called somewhere else (with the save_post action). 我发现我的函数在其他地方被调用(使用save_post动作)。 Because of this, the function didn't have access to the $emailList variable.
因此,该函数无权访问$ emailList变量。 I moved everything (except for the add_action()) inside the function and everything worked great!
我将所有内容(除了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.