Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 如何按计划的时间间隔自动运行函数?_Wordpress_Cron_Wordpress Hook - Fatal编程技术网

Wordpress 如何按计划的时间间隔自动运行函数?

Wordpress 如何按计划的时间间隔自动运行函数?,wordpress,cron,wordpress-hook,Wordpress,Cron,Wordpress Hook,我正在为Wordpress网站开发一个插件,我希望在每个月的第一天生成一份报告,并将其发送给用户/管理员 在服务器中创建Cron作业将是一个完美的解决方案,但在以编程方式创建Cron作业方面没有什么障碍,因为过程在服务器之间是不同的(我猜)。 因此,我考虑使用WordPress函数,而不是在执行此任务的服务器中创建Cron作业 如果你们有任何想法,一定要告诉我。我自己找到了解决方案。我希望这对其他人有帮助 function do_this_in_an_hour() { $postid = "

我正在为Wordpress网站开发一个插件,我希望在每个月的第一天生成一份报告,并将其发送给用户/管理员

在服务器中创建Cron作业将是一个完美的解决方案,但在以编程方式创建Cron作业方面没有什么障碍,因为过程在服务器之间是不同的(我猜)。 因此,我考虑使用WordPress函数,而不是在执行此任务的服务器中创建Cron作业


如果你们有任何想法,一定要告诉我。

我自己找到了解决方案。我希望这对其他人有帮助

function do_this_in_an_hour() {

 $postid = "1"; //Supply post-Id here $post->ID.
    wp_update_post(array(
        'ID'    =>  $postid,
        'post_status'   =>  'draft'
        ));     
}
add_action( 'my_new_event','do_this_in_an_hour' );

wp_schedule_single_event( time() + 7200, 'my_new_event' );

// time() + 7200 = two hour from now.

我自己找到了解决办法。我希望这对其他人有帮助

function do_this_in_an_hour() {

 $postid = "1"; //Supply post-Id here $post->ID.
    wp_update_post(array(
        'ID'    =>  $postid,
        'post_status'   =>  'draft'
        ));     
}
add_action( 'my_new_event','do_this_in_an_hour' );

wp_schedule_single_event( time() + 7200, 'my_new_event' );

// time() + 7200 = two hour from now.

如果间隔时间已过,则它们仅在加载WordPress时运行

add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 60,
            'display'   => __( 'Every 1 Minutes', 'textdomain' )
    );
    return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
    wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}

// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
    $content = "some text here";
    $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/wordpressrootfolder/".time()."-myText.txt","wb");
    fwrite($fp,$content);
    fclose($fp);
}

如果间隔时间已过,则它们仅在加载WordPress时运行

add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 60,
            'display'   => __( 'Every 1 Minutes', 'textdomain' )
    );
    return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
    wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}

// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
    $content = "some text here";
    $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/wordpressrootfolder/".time()."-myText.txt","wb");
    fwrite($fp,$content);
    fclose($fp);
}

这两个答案是相似的,而我想运行的功能,即使网站没有加载。有没有其他方法可以在不使用服务器Cron作业的情况下实现这一点?在您的服务器中设置wget-q-O->/dev/null 2>&1 Cron任务集,因为配置Cron作业因服务器而异,我想在不使用服务器Cron作业的情况下自动执行一个函数。这两个答案是相似的,但我希望即使网站未加载,该函数也能运行。有没有其他方法可以在不使用服务器Cron作业的情况下实现这一点?服务器中的wget-q-O->/dev/null 2>&1 Cron任务集由于配置Cron作业因服务器而异,因此我想在不使用服务器Cron作业的情况下自动化一个功能。这不是一个真正的解决方案,更像是一种变通方法。这将只在WordPress加载时运行。这不是一个真正的解决方案,更多的是一种解决方法。这将仅在加载WordPress时运行。