cron job in wordpress

谁都会走 提交于 2020-01-13 07:30:28

问题


I want to run a cron job in wordpress using the default function. I want set the time to run every 15 days. How can I set the the for this?

 function prefix_deactivation()
{
    wp_clear_scheduled_hook('prefix_hourly_event_hook');
}
register_deactivation_hook(__FILE__, 'prefix_deactivation');
function prefix_activation()
{
    wp_schedule_event(time(), 'everyminute', 'prefix_hourly_event_hook');
}
register_activation_hook(__FILE__, 'prefix_activation');

/* On activation, set a time, frequency and name of an action hook to be scheduled.  */
function prefix_do_this_hourly()
{
// do something every hour
    cronjob_options();
}
function cronjob_options()
{
    /* your job  */
}
add_action('prefix_hourly_event_hook', 'prefix_do_this_hourly');

回答1:


try this code

add_filter( 'cron_schedules', 'cron_add' );

 function cron_add( $schedules ) {

    $schedules = array(
        'everyminute'     => array( 'interval' => 129600,  
                         'display' => __( 'Every Minute' )
                          ),
    );
    return $schedules;
 }



回答2:


Hope you will get perfact answer for cron job in wordpress

 <?php

     add_filter( 'cron_schedules', 'cron_add_everyday' );

     function cron_add_everyday( $schedules ) {
        // Adds once everyday to the existing schedules.
        $schedules = array(
            'everyday'     => array( 'interval' => 86400*1, 
             //set cron time you want call 86400 for 1 day  change 1 as per your days...
                                                       'display' => __( 'Every Day' )
                                                      ),
        );
        return $schedules;
     }
    ?>



回答3:


<?php

     add_filter( 'cron_schedules', 'cron_add_day' );

     function cron_add_day( $schedules ) {
        $schedules = array(
            'day'     => array( 'interval' => 86400*1, 

                               'display' => __( 'Every Day' )
                             ),
        );
        return $schedules;
     }
    ?>



回答4:


    add_filter( 'cron_schedules', 'cron_add_everyminute' );

 function cron_add_everyminute( $schedules ) {

    $schedules = array(
        'everyminute'     => array( 'interval' => 60*60*24*15,  
                                                   'display' => __( 'Every Minute' )
                                                  ),
    );
    return $schedules;
 }


来源:https://stackoverflow.com/questions/24676513/cron-job-in-wordpress

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!