Wordpress schedule daily cron run only after first visit? - wordpress

function cronstarter_activation() {
if( !wp_next_scheduled( 'mycronjob' ) ) {
wp_schedule_event( strtotime( '00:02:00' ), 'daily', 'mycronjob' );
}
}
add_action('wp', 'cronstarter_activation');
function my_repeat_function() {
//My code the same information to database
}
add_action ('mycronjob', 'my_repeat_function');
Everything works great.
My problem is that I have php code in sidebar to recieve the storaged information from the database that saved from cron_job,
and the first time that a visitor visits the site in the new day after the 00:02:00 the recieved information is the previous day information.
I think that the first visitor of day, first see the sidebar's result, which is the previous day information, and after activate the cronjob.
Is that true? How can I run the cronjob without needed the first visitor?

Yes WP Cron have big caveat is that it requires someone to visit the site in order to run. For example, if you schedule your job to run at 2AM but no one visits at 2AM, it won’t run at 2AM. It will run when someone visits the site after 2AM.
More info Official : https://codex.wordpress.org/Function_Reference/wp_schedule_event
More info public blog:
http://brianshim.com/webtricks/cron-job-wordpress/
For more control on cron job use schedule at system level:
Use PHP to create, edit and delete crontab jobs?
Setup/configuration will differ based on your server & hosting type.

Related

Scheduled Actions Disable Logs By Group - Wordpress 6.0.3

I know that to reduce the days and eliminate the records, this is done
function wca_reduce_action_scheduler_retention() {
return WEEK_IN_SECONDS;
}
add_filter( 'action_scheduler_retention_period', 'wca_reduce_action_scheduler_retention' );
However what I need is to discard the logs of the group rocket-preload
To be more precise it skips saving logs for that group. Since I don't need them.
This is possible?
This on the Wordpress Scheduled Actions page (/wp-admin/tools.php?page=action-scheduler)

Check if the specific cron-job is active

I'm wondering whether if I can check a specific cron job is working or not. For example, I would like to send myself an email if the wpse_twicedaily_cron and wpse_oncedaily_cron is not active/ running in the WP.
I can setup the email, but I just need to figure out how to detect if certain cron job is active. I will need to check if the cron jobs are working every 5 minutes.
Thank you!
To view all scheduled tasks, you can use the code below that utilizes _get_cron_array(). Sample code is taken from here
function bl_print_tasks() {
echo '<pre>'; print_r( _get_cron_array() ); echo '</pre>';
}
bl_print_tasks();
You can then create your hook and function and add it as a cron to wordpress.
add_action( 'wpb_custom_cron', 'wpb_custom_cron_func' );
function wpb_custom_cron_func() {
// Check whatever here and then email
wp_mail( 'you#yourdomain.com', 'Cron Alert Email', 'Automatic scheduled email from WordPress to test certain cronjobs');
}
To add your own cron or simply see / trigger other crons, you can use WP Control plugin. Here is how to add your cron from Tools » Cron Events
Screenshot and sample mail code taken from here

cronjob for wordpress runs infinitely

I have this code:
// set up cron job
wp_schedule_event(time(), 'hourly', 'test_event');
add_action('test_event', 'testFunction');
function testFunction()
{
$query = new WP_Query(array(
'post_type' => 'tour',
'post_status' => 'publish'
));
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
echo $post_id . "a<br>";
}
}
And of course, I disabled the Wp cronjob define('DISABLE_WP_CRON', true);.
When I go to http://my-domain/wp/wp-cron.php?doing_wp_cron in the browser (the domain is local, so can't set the real cronjob in the crontab yet with wget), it outputs post IDs multiple times, like this:
1
2
3
1
2
3
Sometimes even more, and sometimes it causes memory error. It seems to me like the cronjob is running infinitely. I can't figure out why.
Not a WP pro, so any suggestion is appreciated!
Thanks.
wp_schedule_event registers a cron task with your WordPress installation.
So, what you're doing here, is every time you're running your cron you are telling WordPress to run your function an additional time to the last time.
If you've visited the cron page 10 times then your cron will be running 10 times per visit.
You can unschedule the event using wp_unschedule_event if you need to.
When you call wp_schedule_event, a new event is registered. You should only call that function once (events persist through multiple visits, globally), the documentation recommends scheduling after plugin activation.
"To schedule an hourly event in a plugin, call wp_schedule_event on activation (otherwise you will end up with a lot of scheduled events!)"
You'll want to use wp_unschedule_event to clear your current bindings, then call the function once. If it is part of your theme, use them activation and toggle your theme to something else and back.

run a piece of code once in a day in wordpress

i was working in a wordpress registration plugin. i stucked in expiry of the user. actually i want to expire the member after one year of his/her registration. and i want to notify them via email before 1 month of their expiry. i am using add_action('init','my function name') to check how many of the user is going to expire after a month and also to send the mail. bt this action hook will run every time a user visits the site which will make my site too slow to load everytime a user will visit. so i want something dat will make this code run once in a day. e.g. when the first user visit the site this code will run and for the whole remaining day this code will not be invoke no matter how many user will visit the website.
Wordpress has a built-in function/API that just do exactly what you want - doing something every day/hour/any interval you specify.
http://codex.wordpress.org/Function_Reference/wp_schedule_event
Taken shamelessly from the above page
add_action( 'wp', 'prefix_setup_schedule' );
/**
* On an early action hook, check if the hook is scheduled - if not, schedule it.
*/
function prefix_setup_schedule() {
if ( ! wp_next_scheduled( 'prefix_daily_event' ) ) {
wp_schedule_event( time(), 'daily', 'prefix_daily_event');
}
}
add_action( 'prefix_daily_event', 'prefix_do_this_daily' );
/**
* On the scheduled action hook, run a function.
*/
function prefix_do_this_daily() {
// check every user and see if their account is expiring, if yes, send your email.
}
prefix_ is presumably to ensure there will be no collision with other plugins, so I suggest you to change this to something unique.
See http://wp.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress/ if you want to know more.

Drupal module to control user post frequency?

We've been having a new type of spam-bot this week at PortableApps.com which posts at a rate of about 10 comments a minute and doesn't seem to stop - at least the first hour or so (we've always stopped it within that time so far). We've had them about a dozen times in the last week - sometimes stopping it at 50 or 60, sometimes up to 250 or 300. We're working to stop it and other spam bots as much as possible, but at the moment it's still a real pest.
I was wondering whether in the mean time whether there's any sort of module to control the frequency a user can post at to e.g. 50 an hour or something like 10 in an hour for new users. That at least would mean that instead of having to clear up 300 comments 50 at a time in admin/content/comment we'd have a smaller number to clear. (A module to add a page to delete all content by a user and block them would also be helpful!)
I believe that there's a plugin to do this available for WordPress, but can't find any such thing for Drupal.
For your second question, i would have a look at the code of the User Delete module (click).
The module also disables the user account and unpublished all nodes/comments from a certain user. By extending the code, you could easily create another possibility to unpublish + delete all nodes/comments from a certain user and blocking the account.
After the unpublish code in the module, you should just put delete code (in sql if the module is selecting by a sql-query or by using the drupal delete functions).
Another option would be so make a view (using the view module) only to be viewed by administrators, where you choose a certain user using the filters and then lists his/her posts. Then in the node-contenttype.tpl.php you place a button that calls a function which deletes all nodes/comments and the user.
First problem (post frequency)
I've been thinking about the comment post limit. If I remember correctly Drupal stores comments in a seperate table and has comment specific functions.
I'd create a new module and using the comment_nodeapi function i would check in the operation 'insert' how much comments the current user has already made within a certain timeframe.
To check this I would write a custom sql query on the database which takes the count of alle comments made by uid where the post_date is larger then NOW-1hour. If that count is larger then 10 or 15 or whatever post frequency you want then you give a message back to the user. You can retrieve the user id and name by using the global $user variable.
(example: print $user->name;)
You have to check on your own for the sql query but here's some code when you have the amount:
<?php
function comment_nodeapi(&$node, $op, $arg = 0) {
switch ($op) {
case 'insert':
//PLACE HERE THE SQL TO GET THE COUNT
if($count > 15){
$repeat = FALSE;
$type = 'status'
drupal_set_message("You have reached the comment limit for this time.", $type, $repeat);
break;
}else{
db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $node->nid, $node->changed, $node->uid);
break;
}
}
}
?>
(this code has not been tested so no guarantees, but this should put you on the right track)
I would suggest something like Mollom (from the creator of Drupal). It scans the message for known spam pattern/keywords/... and if this scan fails, it displays a CAPTCHA to the user to make sure that it's a real human that wants to enter content that has the same properties like spam.
They offer a free service and some paid solutions. We are using it for some customers and it's worth the money. It also integrates very well in Drupal.
Comment Limit is probably what you need.
http://drupal.org/project/spam
http://drupal.org/project/antispam - with akismet support

Resources