You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现用户角色自动过期:注册两周后从Trial转Expired

Solution for Auto-Updating User Roles from Trial to Expired After 2 Weeks (Ultimate Member)

Hey Amanda! I’ve worked with Ultimate Member on similar role automation tasks before, and since the built-in tools aren’t working for you, let’s build a custom solution using WordPress core functions and hooks. Here’s a step-by-step implementation:

1. Ensure We Track User Registration Dates

First, we need a reliable timestamp for when each user registered. While Ultimate Member might store this, we can explicitly save it to user meta on registration to avoid inconsistencies:

// Save registration timestamp when a new user signs up
add_action('user_register', 'um_save_registration_timestamp');
function um_save_registration_timestamp($user_id) {
    if (!get_user_meta($user_id, 'registration_timestamp', true)) {
        update_user_meta($user_id, 'registration_timestamp', time());
    }
}

2. Create the Role Expiration Function

This function will find all users with the Trial role, check if their registration date is older than 14 days, and update their role to Expired:

// Update Trial users to Expired after 14 days
function um_expire_trial_users() {
    // Replace these with your actual role slugs (check Ultimate Member → Roles for slugs)
    $trial_role = 'trial';
    $expired_role = 'expired';
    
    // Calculate timestamp for 14 days ago
    $cutoff = time() - (14 * 24 * 60 * 60);
    
    // Query eligible Trial users
    $args = array(
        'role' => $trial_role,
        'meta_query' => array(
            array(
                'key' => 'registration_timestamp',
                'value' => $cutoff,
                'compare' => '<=',
                'type' => 'NUMERIC'
            )
        ),
        'fields' => 'ID'
    );
    
    $trial_users = get_users($args);
    
    // Update each user's role
    foreach ($trial_users as $user_id) {
        $user = new WP_User($user_id);
        $user->set_role($expired_role);
        
        // Optional: Add extra actions like sending an expiration email
        // wp_mail(get_userdata($user_id)->user_email, 'Your Trial Has Expired', 'Your account role has been updated to Expired.');
    }
}

Pro Tip: You can find your role slugs by visiting Ultimate Member → Roles, then checking the URL of each role (the last segment is the slug).

3. Set Up a Scheduled Cron Job

We need to run the expiration function regularly—once per day is more than enough for this use case:

// Register the daily cron job
add_action('init', 'um_register_expire_cron');
function um_register_expire_cron() {
    if (!wp_next_scheduled('um_daily_expire_check')) {
        wp_schedule_event(time(), 'daily', 'um_daily_expire_check');
    }
}

// Hook the expiration function to the cron event
add_action('um_daily_expire_check', 'um_expire_trial_users');

// Clean up the cron job if you deactivate the theme/plugin
register_deactivation_hook(__FILE__, 'um_remove_expire_cron');
function um_remove_expire_cron() {
    wp_clear_scheduled_hook('um_daily_expire_check');
}

4. How to Add This Code

  • Option 1: Paste all the code into your child theme’s functions.php file (using a child theme prevents losing changes when the parent theme updates).
  • Option 2: Create a custom plugin: Make a folder wp-content/plugins/um-trial-expiration, add a file um-trial-expiration.php inside it, paste the code, then activate the plugin from your WordPress dashboard.

5. Test the Function Quickly

To avoid waiting 14 days to test:

  1. Create a test user with the Trial role.
  2. Manually set their registration_timestamp meta value to a timestamp older than 14 days (use a timestamp converter tool to get a past value).
  3. Temporarily add do_action('um_daily_expire_check'); to your functions.php, load any page on your site, then remove the line.
  4. Check the test user’s role—it should now be Expired.

This solution uses lightweight WordPress core functionality, so it’s fully compatible with Ultimate Member. Let me know if you hit any snags with role slugs or cron setup!

内容的提问来源于stack exchange,提问作者Amanda Kelly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:33:28