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

如何按预设时间显示/隐藏Gravity Forms?实现每日定时启停表单功能

How to Restrict Gravity Forms to Show Only Daily Between 10:00 AM and 10:15 AM

Great question! Gravity Forms' built-in Schedule Form feature works perfectly for one-time date ranges, but it doesn’t handle recurring daily time windows out of the box. Let’s fix that with some custom code—no extra plugins required (though using a code manager will make things safer and easier).

Option 1: Disable the Form (Show "Form Closed" Message)

This method uses Gravity Forms' native form status system to disable the form outside your time window, displaying the default "This form is currently closed" message to users.

Add this code to your site (we recommend using a plugin like Code Snippets instead of editing your theme’s functions.php directly to avoid losing changes when your theme updates):

add_filter( 'gform_is_form_enabled', 'restrict_gform_to_daily_time_slot', 10, 2 );
function restrict_gform_to_daily_time_slot( $is_enabled, $form_id ) {
    // Replace 123 with your actual form ID
    $target_form_id = 123;
    
    // Skip logic for all other forms
    if ( $form_id !== $target_form_id ) {
        return $is_enabled;
    }
    
    // Get current time using WordPress's configured timezone (critical for accuracy!)
    $current_timestamp = current_time( 'timestamp' );
    $current_hour = (int) date( 'H', $current_timestamp );
    $current_minute = (int) date( 'i', $current_timestamp );
    
    // Define your allowed time window (10:00 AM to 10:15 AM)
    $start_hour = 10;
    $start_minute = 0;
    $end_hour = 10;
    $end_minute = 15;
    
    // Convert times to total minutes for simple, error-free comparison
    $current_total_minutes = ( $current_hour * 60 ) + $current_minute;
    $window_start_minutes = ( $start_hour * 60 ) + $start_minute;
    $window_end_minutes = ( $end_hour * 60 ) + $end_minute;
    
    // Check if current time falls within the allowed window
    if ( $current_total_minutes >= $window_start_minutes && $current_total_minutes < $window_end_minutes ) {
        return true; // Enable the form
    } else {
        return false; // Disable the form
    }
}

Option 2: Completely Hide the Form (No Message Shown)

If you want the form to disappear entirely outside the time window (instead of showing a closed message), use this alternative code. It taps into the gform_pre_render hook to stop the form from rendering at all:

add_filter( 'gform_pre_render', 'hide_gform_outside_daily_time_slot' );
function hide_gform_outside_daily_time_slot( $form ) {
    // Replace 123 with your actual form ID
    $target_form_id = 123;
    
    if ( $form['id'] !== $target_form_id ) {
        return $form;
    }
    
    $current_timestamp = current_time( 'timestamp' );
    $current_hour = (int) date( 'H', $current_timestamp );
    $current_minute = (int) date( 'i', $current_timestamp );
    
    $window_start_minutes = 10 * 60; // 10:00 AM
    $window_end_minutes = 10 * 60 + 15; // 10:15 AM
    $current_total_minutes = ( $current_hour * 60 ) + $current_minute;
    
    // Hide the form entirely if outside the window
    if ( $current_total_minutes < $window_start_minutes || $current_total_minutes >= $window_end_minutes ) {
        return '';
    }
    
    return $form;
}

Key Tips to Get It Right:

  • Timezone Check: Double-check your WordPress site’s timezone (Go to Settings > General in your WP Admin). The code uses current_time() which respects this setting, so your time window will align with your local timezone.
  • Form ID: Replace 123 with the ID of your Gravity Form. You can find the form ID in the Gravity Forms dashboard—it’s shown next to each form name.
  • Code Safety: Using a code snippet plugin is the safest way to add custom code—you won’t lose changes when updating your theme or WordPress core.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:08:52