如何可靠追踪WordPress主题活跃时长以实现条件通知?
Got it, let's break down the best way to track your WordPress theme's active duration (not just installation time) to hit your notification trigger goal—here's a robust, efficient approach that integrates smoothly with your existing setup:
Core Idea: Use WordPress Options for Persistent Tracking
WordPress's built-in options system is perfect here—it's lightweight, persistent, and easy to work with. We'll store two key pieces of data: the last time we checked the theme's active state, and the total accumulated active seconds.
Step 1: Initialize Tracking on Theme Activation
First, set up the initial tracking data when the theme is switched to. Use the after_switch_theme hook to create the option if it doesn't exist (avoids overwriting data if the theme is re-activated):
add_action('after_switch_theme', 'my_theme_init_active_duration'); function my_theme_init_active_duration() { if (!get_option('my_theme_active_duration')) { update_option('my_theme_active_duration', [ 'start_time' => current_time('timestamp'), 'total_active' => 0 ]); } }
Step 2: Update Active Duration on Site Activity
Instead of relying on cron (which can be unreliable or overkill), update the duration whenever the site is accessed (via the init hook). This ensures we only track time when the theme is actually being used (i.e., someone visits the site):
add_action('init', 'my_theme_update_active_duration'); function my_theme_update_active_duration() { $duration_data = get_option('my_theme_active_duration'); if (!$duration_data) return; $current_time = current_time('timestamp'); $time_elapsed = $current_time - $duration_data['start_time']; // Only add positive time to handle server time drift edge cases if ($time_elapsed > 0) { $duration_data['total_active'] += $time_elapsed; $duration_data['start_time'] = $current_time; // Reset the clock for next check update_option('my_theme_active_duration', $duration_data); } }
Optimization for High-Traffic Sites
If your site gets a lot of hits, reduce database writes by only updating every 5 minutes (300 seconds):
add_action('init', 'my_theme_update_active_duration'); function my_theme_update_active_duration() { $duration_data = get_option('my_theme_active_duration'); if (!$duration_data) return; $current_time = current_time('timestamp'); $time_elapsed = $current_time - $duration_data['start_time']; // Update only if 5+ minutes have passed if ($time_elapsed > 300) { $duration_data['total_active'] += $time_elapsed; $duration_data['start_time'] = $current_time; update_option('my_theme_active_duration', $duration_data); } }
Step 3: Finalize Duration When Theme is Switched
Capture the last bit of active time if the user switches to another theme using the switch_theme hook:
add_action('switch_theme', 'my_theme_finalize_active_duration'); function my_theme_finalize_active_duration() { $duration_data = get_option('my_theme_active_duration'); if (!$duration_data) return; $current_time = current_time('timestamp'); $time_elapsed = $current_time - $duration_data['start_time']; if ($time_elapsed > 0) { $duration_data['total_active'] += $time_elapsed; update_option('my_theme_active_duration', $duration_data); } }
Step 4: Check Trigger Conditions for Notifications
Integrate this with your existing post-count logic to check when both conditions are met. For example, trigger the check when a post is published:
add_action('publish_post', 'my_theme_check_notification_trigger'); function my_theme_check_notification_trigger($post_id) { // Replace with your required values $required_posts = 5; // x posts $required_hours = 24; // y hours $required_seconds = $required_hours * 3600; // Get published post count (adjust post type if needed) $published_count = wp_count_posts('post')->publish; // Get total active duration $duration_data = get_option('my_theme_active_duration'); $total_active = $duration_data ? $duration_data['total_active'] : 0; // Trigger notification if both conditions are satisfied if ($published_count >= $required_posts && $total_active >= $required_seconds) { // Add your existing notification logic here // Example: Set a flag to show the notification update_option('my_theme_show_notification', true); } }
Key Notes
- Use
current_time('timestamp')instead oftime()to respect your site's timezone settings. - Prefix your options (like
my_theme_) to avoid conflicts with other plugins/themes. - To reset tracking, delete the option via WP CLI:
wp option delete my_theme_active_durationor add a reset button in your theme's settings.
内容的提问来源于stack exchange,提问作者Jeremy Muckel

