WordPress Development Asked by dc09 on January 27, 2021
I need to set up a cron to change role of a user at a specific time. For example- At 8 PM everyday, the role of a specific user should be changed from “Customer” to “Editor”
How to accomplish this in wordpress?
UPDATE:
I have scheduled the function as follows but it’s not running at the time set:
if( !wp_next_scheduled( 'import_into_db' ) ) {
wp_schedule_event( strtotime('12:04:00'), 'daily', 'import_into_db' );
function import_into_db(){
$u = new WP_User(3);
$u->set_role('editor');
}
add_action('wp', 'import_into_db');
}
The functions is running fine independently (without scheduling) but doesn’t run when scheduled. Cron is working fine on my install and time is set as per UTC. What’s wrong in the scheduling part of my code?
You can do something like this
add_action("after_switch_theme", "schedule_cron_job"); // hook the schedule on theme switch or plugin activation based on the your usage also switch your theme after putting this on functions.php
function schedule_cron_job(){
if (! wp_next_scheduled ( 'import_into_db' )) {
wp_schedule_event(strtotime('12:04:00'), 'daily', 'import_into_db');
}
}
add_action('import_into_db', 'your_cron_job'); // You were hooking to wp
function your_cron_job(){
$u = new WP_User(3);
$u->set_role('editor');
}
You can use cron manager plugins to check if cron job is scheduled or not.
Answered by Bikash Waiba on January 27, 2021
You need to get the user's ID, create a WP_User instance, and with that, you can change the role for a specific user:
$u = new WP_User(3);
$u->set_role('editor');
You can then use a custom interval to schedule this. How to do that has been answered here: Schedule event at specific time every day
Answered by NightHawk on January 27, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP