【发布时间】:2017-09-26 11:02:43
【问题描述】:
管理员或编辑可以通过我们为个人定制的 WordPress 插件向用户发送电子邮件,以便单独发送电子邮件,这可以使页面等待一段时间以完成邮寄过程。计划是多线程将用户返回到页面并让电子邮件在服务器上进行发送。问题似乎是无法从线程的 run() 函数访问 WordPress 的 wp_mail() 函数。调用 wp-load.php 似乎无法访问 WordPress 函数。
以下是线程邮件程序的简化版本。我们如何使用 Test_Mailer 线程中的 wp-mail() 函数?
class Test_Mailer extends Thread {
private $email;
public function __construct($emails, $subject, $message) {
$this->emails = $emails;
$this->subject = $subject;
$this->message = $message;
}
public function run() {
require( '../../../wp-load.php' );
foreach($this->emails as $email)
wp_mail( $email, $this->subject, $this->message);
}
}
$test_mailer = new test_mailer(array('test@test.com'), 'Subject', 'Message');
$test_mailer->start();
编辑:如果我尝试访问 WordPress 函数,如下所示没有任何反应,这让我认为 WP 未加载,但我在 PHP 错误日志中没有收到任何错误。
class Test_Thread extends Thread {
public function run() {
update_option('test', 'two');
}
}
update_option('test', 'one');
$test_thread = new test_mailer();
$test_thread->start();
// test option is set to 'one'
【问题讨论】: