这里有人知道或可以指导我如何在新用户在其网站上注册 36 小时后向 WordPress 管理员发送电子邮件吗?
这就是我想要完成的:
- 在数据库中查找所有用户
- 检查用户是否具有特定角色
- 检查其中一些用户是否已经注册了 36 小时
- 使用 WP_cron 设置这些(我将每小时使用一次以确保捕获所有这些用户)
这是我目前正在做的代码,但每当我想获得已注册 36 小时的用户时,我总是迷路。
/**
* SEND A NOTIFICATION AFTER 36 HOURS
*/
function myprefix_custom_cron_schedule( $schedules ) {
$schedules['every_hour'] = array(
'interval' => 3600, // Every hour
'display' => __( 'Every hour' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
//Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'qd_cron_hook' ) ) {
wp_schedule_event( time(), 'every_hour', 'qd_cron_hook' );
}
///Hook into that action that'll fire every hour
add_action( 'qd_cron_hook', 'qd_cron_function' );
//create function, that runs on cron
function qd_cron_function() {
$args = array(
'role' => 'business-pending',
'fields' => array( 'ID', 'user_registered' ),
);
$users = get_users( $args );
foreach($users as $user){
$user_regDate = $user->user_registered;
$dateNow = gmdate("Y-m-d H:i:s");
This is where I get lost. I'm assuming that I have to subtract the time now to the user_registered value and then make a condition to only display users that has registered in 36 hours.
$emailto = get_option('admin_email');
$subject = 'Please activate this user';
$message = 'This user has not been activated.';
wp_mail( $emailto, $subject, $message );
}
任何帮助将不胜感激。非常感谢!
最佳答案
请检查这个答案:
https://wordpress.stackexchange.com/questions/174700/wp-user-query-users-by-registered-date
你在哪里:
$args = array(
'role' => 'business-pending',
'fields' => array( 'ID', 'user_registered' ),
);
$users = get_users( $args );
您可以替换为:
$args = array(
'role' => 'business-pending',
'fields' => array( 'ID', 'user_registered' ),
'date_query' => array(
array(
'before' => date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s")." -36 hours")), // registered more than 36 hours ago
'inclusive' => true,
),
),
);
$users = get_users( $args );
建议:
请注意,使用您的代码,您将获得超过 36 小时的用户注册。很可能您应该在数据库中使用一些标志来指示哪些用户的电子邮件已经发送。
关于sql - 新用户在 WordPress 注册后 36 小时向管理员发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50379750/