How to restrict certain dates in Contact Form 7 date picker widget?

I’m running a WordPress website for our college where people can schedule facility visits. We’re using Contact Form 7 to build a booking form that includes a date picker for visitors to choose their preferred visit date.

I need to prevent certain dates from being clickable in the date picker. Our facility is closed on holidays and some other days when staff isn’t available, so these shouldn’t be options for booking.

Can this be done with the plugin settings, or do I need custom code or another plugin? I saw a non-WordPress booking site that successfully blocks unavailable dates in their jQuery date picker, so it seems doable.

I tried implementing this code snippet I found, setting the date picker ID to “#BlockedDates” and adding it to my theme’s header file:

<?php if ($post->ID==25) { ?>
<script type="text/javascript">
    var blockedDates = ["15-3-2024", "16-3-2024", "17-3-2024", "18-3-2024", "25-3-2024", "26-3-2024", "1-4-2024", "8-4-2024", "15-4-2024", "22-4-2024", "29-4-2024"];

    jQuery(function($){
        $( "#BlockedDates" ).datepicker({
            minDate: 3,
            beforeShowDay: function(date) {
                dateString = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
                if ($.inArray(dateString, blockedDates) == -1) {
                    return [true, ""];
                } else {
                    return [false, "", "Not Available"];
                }
            }
        });
    });
</script>

The restricted dates are still selectable though. What am I missing?

Why wrestle with CF7’s limitations when you can automate this whole thing?

I had the same headache with our office booking system. Instead of fighting CF7, I built a workflow in Latenode that handles everything. Form gets submitted, it checks our blocked dates database automatically, and shoots back a nice email with other options if someone picks a bad date.

Best part? Connect it to your calendar and it blocks dates when staff schedules change. No more manually updating code arrays.

Latenode works with any form system - ditch CF7 for something simpler and let automation handle the logic. I’ve got Slack notifications too, so we know about bookings instantly.

Saved me hours of debugging JavaScript conflicts and your booking data actually gets organized properly instead of sitting in CF7’s crappy storage.

Check it out: https://latenode.com

The Problem:

You’re trying to prevent specific dates from being selectable in a Contact Form 7 date picker on your WordPress website. You’ve added a JavaScript snippet to your theme’s header, but the blocked dates remain selectable.

TL;DR: The Quick Fix:

The issue is likely a combination of incorrect element ID targeting and the timing of your script execution relative to Contact Form 7’s JavaScript. Contact Form 7 dynamically generates IDs, and your script might be running before the datepicker is fully initialized. Correct the element ID and use Contact Form 7’s hooks to ensure your script runs at the right time.

:thinking: Understanding the “Why” (The Root Cause):

Your original code attempts to modify a jQuery datepicker using a hardcoded ID (#BlockedDates). Contact Form 7, however, doesn’t use a standard jQuery UI datepicker. It generates its own datepicker with a dynamically created ID. Your script, placed in the header, runs before Contact Form 7’s datepicker is fully rendered, meaning the element with the ID you’re targeting either doesn’t exist yet or isn’t the correct datepicker. Furthermore, placing your script in the header can cause conflicts because Contact Form 7 loads its own scripts, potentially overwriting your customizations.

:gear: Step-by-Step Guide:

  1. Identify the Correct Date Picker ID: Inspect the HTML source of your Contact Form 7 datepicker field using your browser’s developer tools (usually accessed by pressing F12). Right-click on the datepicker and select “Inspect” or “Inspect Element.” Locate the <input> element representing the datepicker. Note its id attribute. This ID will likely differ from “#BlockedDates”.

  2. Use Contact Form 7 Hooks: Instead of placing your JavaScript in the theme header, use Contact Form 7’s hooks to integrate your code. This ensures your script runs after Contact Form 7 initializes its datepicker. Add the following code to your theme’s functions.php file:

function custom_cf7_datepicker_script() {
    if ( is_page(25) ) { // Only run on page with ID 25
        wp_enqueue_script( 'custom-datepicker', get_stylesheet_directory_uri() . '/custom-datepicker.js', array( 'jquery', 'jquery-ui-datepicker' ), '1.0', true );
    }
}
add_action( 'wp_enqueue_scripts', 'custom_cf7_datepicker_script' );

Create a new file named custom-datepicker.js (or similar) in your theme’s directory and add your Javascript there.

  1. Rewrite your JavaScript: Replace the hardcoded ID in your JavaScript file with the actual ID you identified in Step 1. Ensure your date format (DD-MM-YYYY in your example) matches your WordPress settings. Also, consider using a zero-padded date format like '0' + day instead of just day to handle single-digit days and months.
jQuery(document).ready(function($) {
    var blockedDates = ["15-03-2024", "16-03-2024", "17-03-2024", "18-03-2024", "25-03-2024", "26-03-2024", "01-04-2024", "08-04-2024", "15-04-2024", "22-04-2024", "29-04-2024"];

    var datePickerID = '#your-actual-datepicker-id'; // Replace with the ID from step 1

    $( datePickerID ).datepicker({
        minDate: 3,
        beforeShowDay: function(date) {
            var day = date.getDate();
            var month = date.getMonth() + 1;
            var year = date.getFullYear();
            var dateString = ("0" + day).slice(-2) + "-" + ("0" + month).slice(-2) + "-" + year;

            if ($.inArray(dateString, blockedDates) == -1) {
                return [true, ""];
            } else {
                return [false, "", "Not Available"];
            }
        }
    });
});
  1. Test Thoroughly: Save your changes and test the form to ensure the blocked dates are now correctly prevented from selection.

:mag: Common Pitfalls & What to Check Next:

  • Caching: Clear your browser’s cache and any WordPress caching plugins after making changes.
  • JavaScript Conflicts: If problems persist, temporarily deactivate other plugins that might affect JavaScript functionality.
  • Date Format: Double-check the blockedDates array format matches the datepicker’s expected format, carefully considering the day/month order.
  • jQuery Version: Ensure your theme is including a compatible version of jQuery and jQuery UI datepicker.
  • Consider a Dedicated Plugin: If you frequently need advanced datepicker functionality, consider using a dedicated plugin designed for Contact Form 7.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

CF7’s date field doesn’t use the standard jQuery UI datepicker - it has its own implementation that overrides your custom settings. I hit this same issue setting up appointment scheduling for a medical office. The Contact Form 7 Datepicker plugin solved it for me since it gives you way more control. Install it, then add your custom JavaScript in the contact form’s additional settings tab instead of your theme header. You might also need to target the actual input field ID that CF7 generates - it’s usually different than what you’d expect. Inspect the element in your browser to find the right selector. CF7 auto-generates IDs that don’t match your custom naming. Another option that worked well was switching to a dedicated booking plugin like Booking Calendar or WP Simple Booking Calendar. They’ve got built-in date restrictions without needing custom code.

CF7 overwrites your datepicker settings because it loads after your custom script. I ran into this exact issue building a conference room booking system last month. You need to use CF7’s hooks instead of regular jQuery document ready.

Dump this in your theme’s functions.php (not the header):

function custom_cf7_datepicker_script() {
    if (is_page(25)) {
        wp_add_inline_script('contact-form-7', 'your datepicker code here');
    }
}
add_action('wp_enqueue_scripts', 'custom_cf7_datepicker_script');

This loads your script with CF7’s assets. Also check that your date format matches WordPress admin locale settings. European formats clash with jQuery datepicker’s default MM-DD-YYYY format.

Your code looks right, but it’s a timing problem. Contact Form 7 loads its datepicker after the DOM loads, so your script runs before CF7 sets up its datepicker. I hit this same issue setting up equipment reservations for our lab. Wrap your datepicker code in a document ready function with a delay: jQuery(document).ready(function($) { setTimeout(function() { /* your datepicker code here */ }, 1000); }); instead of jQuery(function($). Also check that your date format matches what jQuery wants—day/month order can mess things up. You might need to pad single digits with zeros like “15-03-2024” not “15-3-2024”. Your beforeShowDay function should work once you fix the timing.

CF7 reinitializes the datepicker after your script runs, wiping out your custom settings. I faced a similar issue while building a rental system last year. Here’s what you can do: Move your script to the footer and add a delay using setTimeout(function() { /* your datepicker code */ }, 500) to ensure it loads after CF7. Additionally, double-check that your form is indeed on post ID 25; if this fails, your script won’t execute. You can also hook into CF7’s wpcf7mailsent event to reset your datepicker settings. Lastly, ensure your blockedDates format matches the standard JavaScript format; using YYYY-MM-DD can help mitigate locale issues. The setTimeout approach often works but the event hook is cleaner if you’re comfortable with it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.