Run a Change Role Cron Job on WordPress via cPanel

Can someone point me in the right direction here?

I’m attempting to set up a plugin that sets up a cron job on activation to change the user_role of a user after 3 months of their user_registered date. The cron job should be removed when the plugin is deactivated as well.

The plugin code so far is:

<?php
/*
Plugin Name: Cron Job
*/

add_filter( ‘cron_schedules’, ‘example_add_cron_interval’ );

function example_add_cron_interval( $schedules ) {
$schedules[‘five_seconds’] = array(
‘interval’ => 5,
‘display’ => esc_html__( ‘Every Five Seconds’ ),
);

return $schedules;
}

function cron_hook(){
$users = get_users(
array(
‘fields’ => array( ‘ID’, ‘user_registered’ ),
‘role’ => ‘coaching’,
)
);

foreach ( $users as $user ) {

$time = $user->user_registered;

if(strtotime($time) < strtotime(‘-3 months’)) {
$u = new WP_User( $user->ID );
// Remove role
print_r($u);

$u->remove_role( ‘coaching’ );

// Add role
$u->add_role( ‘platinum’ );
}
}
}

//echo ‘<pre>’; print_r( _get_cron_array() ); echo ‘</pre>’;

public static function activate() {
wp_schedule_event( time(), ‘five_seconds’, ‘cron_job’ );
}

public static function deactivate() {
wp_clear_scheduled_hook(‘cron_job’);
}

I’m running wp-cron.php through cPanel (currently set to five_seconds for testing).

Read more here:: Run a Change Role Cron Job on WordPress via cPanel

Leave a Reply

Your email address will not be published. Required fields are marked *