php - wp_schedule_single_event 无法成功运行
问题描述
我为 Wordpress 网站编写了一个 Android 应用程序。我想要一个像电报这样的登录系统。所以我在服务器端创建了一个获取电话号码表单应用程序的 php 文件。
如果数据库中存在具有此电话号码的用户,则创建随机代码并将其作为 post meta 添加到数据库并将其发送给用户。现在我想在 5 分钟后删除帖子元数据。所以我用了“ wp_schedule_single_event
”。但它不起作用。我该如何解决?
我的代码是:
<?php
/*
get phone from app
cheack phone is exist or not
if is exist : create random code and send to app
if not exist create user after that create random code and send to app
if get error send to app text "error"
*/
require_once("../wp-load.php");
require_once("../wp-cron.php");
require_once("../wp-includes/cron.php");
require_once("class.aesCrypt.php");
$tellenc = $_POST['tell'];
$randomcode=generateRandomString();
$postmetavalue="";
$tellenc="xDdW6n3pqbxqELXLsuLleg==";
//decrypt tell
$encryption = new MCrypt();
$tell= $encryption->decrypt($tellenc);
//create meta post key=tell ,value=random code
update_post_meta( 1, $tell, $randomcode );
$user_id = username_exists( $tell );
//if exist user: send to app isexist+random code
if ( $user_id ) {
echo "true{".$randomcode;
wp_schedule_single_event( time() + 60, 'my_new_event' );
}//end if user is exist
// if user is not exist: send to app isnotexist+random code
else {
echo "false{".$randomcode;
//register user as customer
}
// echo $output.= "notExist";
// function genrate random code
function generateRandomString($length = 4) {
$characters = '0123456789';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function do_this_in_an_hour() {
update_post_meta( 1, 'mytest', 'tesstziroq' );
}
add_action( 'my_new_event','do_this_in_an_hour' );
?>
解决方案
推荐阅读
- excel - 如何使用单个公式计算条件中位数数组的平均值?
- javascript - 根据数据库变量生成“n”个 html 输入
- c# - 从 Datagridviewcells 更新 Oracle 表
- r - 提取多边形质心的坐标并按多边形编号对其进行标记
- javascript - 如何在 a 中左右浮动元素标签?
- javascript - ForEach 中的 setTimeout 不会等待给定的特定毫秒
- string - 带有三元运算符的 Java SE 11 字符串最终变量在 Switch Case 表达式中不计为常量变量
- go - 如何启动多个机械工人?
- c++ - 在创建和不创建模板的情况下递增迭代器
- reactjs - react-native-autocomplete-input 中的borderRadius