0

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.

Currently, the below code isn't working. The cron job runs, but the user role does not update.

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).

  • Could you please make your question a bit more specific? You have explained your overall goal and provided code, but it's not clear which part you're struggling with. Is something not working? – WebElaine Aug 6 '18 at 14:32
  • Apologies - Updated question now - currently, the code isn't working. The cron job runs, but the user role does not update. – gromey Aug 6 '18 at 14:43
0

Managed to find the solution.

My finished code is:

register_activation_hook ( __FILE__, 'cron_job' );

function cron_job() 
{
wp_schedule_event( time(), 'hourly', 'hourly_job' );
}

add_action( 'hourly_job', 'cron_job_task' );

function cron_job_task () 
{
    $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 );
            $u->remove_role( 'coaching' );
            $u->add_role( 'platinum' );
        }
    }
}
?>

With a cPanel cron job set hourly.

  • Don't forget to also register your deactivate() function with 'register_deactivation_hook' to clear the scheduled hook when the plugin gets deactivated. – Iceable Aug 6 '18 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.