• Phill

    (@phillsav)


    I have created a custom role using this plugin called ‘guest’.

    I’m running a function to update my role if conditions are met, but it is not working. What additional do I need to do for my role which was created within the plugin? Thanks

    // Function to check and update user role based on End Date
    function update_user_role_based_on_end_date_cron() {
    $users = get_users(array('meta_key' => 'end_date', 'meta_compare' => '<=', 'meta_value' => date('Y-m-d')));
    
    foreach ($users as $user) {
        // Check if the End Date is in the past
        $end_date = get_user_meta($user->ID, 'end_date', true);
        if ($end_date && strtotime($end_date) < time()) {
            // Log a message to the error log
            error_log("Updating role to Guest for user ID: {$user->ID}");
    
            // Update user role to Guest
            $user_object = new WP_User($user->ID);
            $user_object->set_role('guest');
        }
    }
    
    }
    
    // Schedule the cron event every 5 minutes
    function schedule_update_user_role_cron() {
    if (!wp_next_scheduled('update_user_role_event')) {
    wp_schedule_event(time(), 'every_five_minutes', 'update_user_role_event');
    }
    }
    add_action('wp', 'schedule_update_user_role_cron');
    
    // Hook the cron event to run the function
    add_action('update_user_role_event', 'update_user_role_based_on_end_date_cron');
  • The topic ‘Custom Role’ is closed to new replies.