Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 551 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wp_schedule_event() in plugin not scheduling a cron event

#1
I'm creating a WordPress plugin, when the plugin is activated I need a cron job to be scheduled to run every 5 minutes.

Here's my code;

// Register plugin activation hook
function my_plugin_activate() {
if( !wp_next_scheduled( 'my_function_hook' ) ) {
wp_schedule_event( time(), '5', 'my_function_hook' );
}
}
register_activation_hook( __FILE__, 'my_plugin_activate' );

// Register plugin deactivation hook
function my_plugin_deactivate(){
wp_clear_scheduled_hook('my_function_hook');
}
register_deactivation_hook(__FILE__,'my_plugin_deactivate');

// Function I want to run when cron event runs
function my_function(){
//Function code
}
add_action( 'my_function_hook', 'my_function');

When I use this plugin

[To see links please register here]

to check the cron events, nothing has been added, I'm expecting a cron event to be added that runs 'my_function' at 5 minute intervals, I have no errors
Reply

#2
See: [`wp_schedule_event()`](

[To see links please register here]

)

> Valid values for the recurrence are hourly, daily, and twicedaily.
> These can be extended using the ‘cron_schedules’ filter in
> wp_get_schedules().

So you just need to add a custom schedule that runs every 5 minutes.

<?php // Requires PHP 5.4+.

add_filter( 'cron_schedules', function ( $schedules ) {
$schedules['every-5-minutes'] = array(
'interval' => 5 * MINUTE_IN_SECONDS,
'display' => __( 'Every 5 minutes' )
);
return $schedules;
} );

if( ! wp_next_scheduled( 'my_function_hook' ) ) {
wp_schedule_event( time(), 'every-5-minutes', 'my_function_hook' );
}
Reply

#3
As @dingo_d commented, WordPress Cron doesn't work like server Cron. the WordPress Cron runs on page load. It checks the database for a scheduled event and if an event is scheduled it runs the task. So if nobody visits the website in a 5 minute time period *no* job will be run in that period. When someone *does* visit the website, the page load process runs and the scheduled event takes place.

It was setup like this so WordPress would work without needing any particular Cron functionality on the server.

To circumvent this you can use a service that automatically visit your website or you could setup a Cron script on your server to automatically load the page.

Assuming a linux server you ssh into the terminal then write `crontab -e` and hit enter. You'll enter the cron file for setting up cron jobs. Add to the file the following line:

/5 * * * wget -q -O -

[To see links please register here]


substituting `http://yourdomain.com` for your actual website. This ensures your website is visited every 5 minutes.

I grabbed the information on how to do this from

[To see links please register here]

so credit to him and that link has lots more WordPress cron info.
Reply

#4
WP Cron runs, when somebody visits your website.
Thus if nobody visits, the cron never runs.

Now there are 2 solutions:

1. Disable WP Cron, use a real cron job and customize it.

[To see links please register here]


2. Use a custom interval in `wp_schedule_event()`:





function myprefix_custom_cron_schedule( $schedules ) {
$schedules['every_six_hours'] = array(
'interval' => 21600, // Every 6 hours
'display' => __( 'Every 6 hours' ),
);
return $schedules;
}
add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );

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

///Hook into that action that'll fire every six hours
add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );

//create your function, that runs on cron
function myprefix_cron_function() {
//your function...
}


and you can see these tuts

[To see links please register here]


[To see links please register here]


[To see links please register here]


custom Wp cron

[To see links please register here]


[To see links please register here]


[To see links please register here]


[To see links please register here]


[To see links please register here]


[To see links please register here]



cron linux

[To see links please register here]


[To see links please register here]


[To see links please register here]


[google search][1]


[1]: https://www.google.com/search?q=wp%20cro...mp;spell=1
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through