首页 > 解决方案 > 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' );

 ?>

标签: phpandroidwordpress

解决方案


推荐阅读