How to add a jQuery datepicker to your contact form 7

I really love the contact form 7 plugin, but there is 1 big thing missing: a datepicker. A datepicker is an easy way to select a date from a popup calendar. Luckily there is jQuery, and with a little bit of php code it’s possible to add a datepicker to your form.

Introduction on the jQuery datepicker

What is jQuery?

JQuery is one of the best javascript libraries at the moment. Web developers use it to create user friendly interfaces. Thanks to jQuery you only have to add a couple of lines of code to add interaction. If you want to know more about jQuery, please visit www.jquery.com.

About the datepicker

The datepicker is actually a plugin for jQuery. You can download it from jqueryui.com. This jQueryUI is a bundle of useful plugins for jQuery.

Demo

Check out the demo on jqueryui.com. In the “Demos & Documentation” section, click “Datepicker” under “Widgets”:

Datepicker demo

In this demo section you can select your theme (1). This theme will define how your datepicker will look like. Select the desired example (2) and check out the result (3). At this stage you have an idea what you can do with jQuery. For this tutorial, basic functionality will be just fine.

Installation of jQueryUI

Download jQueryUI

To download jQuery AND jqueryUI visit jqueryui.com and go to the “Download” section:

Download jQueryUI

The great thing here is that you can “build” your own download. You can select which options to include / exclude, and also to select a theme.

In this case we’re just going to build a basic package (including all the options). First, select a theme (1) and after that click the “Download” button (2).

It’s also possible to build your own theme, so if can’t find the right theme, you just can create your own here. Just click the “design a custom theme” link above the theme selection (1).

Upload the downloaded folder with FTP

Once you downloaded jQueryUI, unzip it and upload the jquery-ui-1 folder to the root of your website (another location is also fine, but in this example I use the rood folder).

At this point, you can check if everything is fine. Just go to www.yourdomain.com/jquery-ui-1/ and check if you get the following result:

Welcome to jQueryUI

Create your contact page

Add a new “contact page”

The next step is quite simple. Just add a new page and publish this page (without any content). It’s also important to know the page id. There are a couple of ways to find the page id. An easy way is to do the following:

  • Edit your contact page
  • In the url bar you will see the following:
    http://www.mydomain.com/wp-admin/post.php?post=21&action=edit
  • The number after “post=” is the id of the page

Create a new form

Add a new simple contact form. If you don’t know how to do this, please read my Contact Form 7 Step By Step Tutorial.

It’s important to create a new text field:

Text date field

As you can see in the example above, I added a text field with the name “start-date”. Also, don’t forget the id (“id:start-date”), this id will be used by jQuery.

If you should test this right now, you’d see this field is nothing more then a regular text field.

Don’t forget to add the form to the Contact page.

Connect the contact form date field with the jQueryUI datepicker

Okay, we uploaded jQuery, we created a new text field called “start-date”… now let the magic begin!

Changing header.php

Open header.php (you can find this under /wp-content/themes/name-of-your-theme/) with your favorite PHP editor. I work on Mac and I use Panic Coda as a PHP editor.

The following lines of code we are going to add between the <head>-tags.

First of all, we are going to define that this is a php script:

<?php
//jQuery
?>

Next we are going to build an if structure, this script may only run if the current page is our contact page. That’s why we need the page id:

<?php
//jQuery
if($post->ID==21){
    //this code only runs when the current page is our contact page!
}
?>

In the next step we are going to include all the necessary files:

  • the jQueryUI css file
  • the jQuery javascript file
  • the jQueryUI core
  • the jQueryUI datepicker
<?php
//jQuery
if($post->ID==21){
    //this code only runs when the current page is our contact page!
    //add the necessary files
    // --> css
    echo '<link rel="stylesheet" <link rel="stylesheet" href="http://www.mydomain.com/jquery-ui-1/development-bundle/themes/ui-lightness/jquery.ui.all.css">';
    // --> jQuery javascript
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/jquery-1.5.1.js"></script>';
    // --> jQueryUI core
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/ui/jquery.ui.core.js"></script>';
    // --> jQueryUI datepicker
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/ui/jquery.ui.datepicker.js"></script>';
} ?>

Okay, for the moment all the necessary files are loaded only when visiting the contact page. Now let’s add the function to turn our text field into a datepicker:

<?php
//jQuery
if($post->ID==21){
//this code only runs when the current page is our contact page!
    //ADD THE NECESSARY FILES
    // --> css
    echo '<link rel="stylesheet" <link rel="stylesheet" href="http://www.mydomain.com/jquery-ui-1/development-bundle/themes/ui-lightness/jquery.ui.all.css">';
    // --> jQuery javascript
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/jquery-1.5.1.js"></script>';
    // --> jQueryUI core
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/ui/jquery.ui.core.js"></script>';
    // --> jQueryUI datepicker
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/ui/jquery.ui.datepicker.js"></script>';
    //TURN TEXT FIELD INTO DATEPICKER
    echo '<script type="text/javascript">';
    echo 'jQuery(function($){';
    echo '$( "#start-date" ).datepicker({});';
    echo '});';
    echo '</script>';
}
?>

Notice “start-date” is the name of our text field we want to turn into a datepicker. If you save all this and go to the contact page, you should see this datepicker when clicking in the “Start date” field:

Start date

Localisation

One last detail is the language of the datepicker. Standard the language is English but it’s easy to change this. Just add an extra line of code with the language file. In the following example we will add the French language file:

<?php
//jQuery
if($post->ID==21){
    //this code only runs when the current page is our contact page!
    //ADD THE NECESSARY FILES
    // --> css
    echo '<link rel="stylesheet" <link rel="stylesheet" href="http://www.mydomain.com/jquery-ui-1/development-bundle/themes/ui-lightness/jquery.ui.all.css">';
    // --> jQuery javascript
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/jquery-1.5.1.js"></script>';
    // --> jQueryUI core
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/ui/jquery.ui.core.js"></script>';
    // --> jQueryUI datepicker
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/ui/jquery.ui.datepicker.js"></script>';
    //TURN TEXT FIELD INTO DATEPICKER
    echo '<script type="text/javascript">';
    echo 'jQuery(function($){';
    echo '$( "#start-date" ).datepicker({});';
    echo '});';
    echo '</script>';
    //ADD FRENCH LANGUAGE FILE
    echo '<script src="http://www.mydomain.com/jquery-ui-1/development-bundle/ui/i18n/jquery.ui.datepicker-fr.js"></script>';
}
?>

You can find all the language files in the following folder: /jquery-ui-1/development-bundle/ui/i18n/

Conclusion

As you can see it’s not that difficult to add a datepicker to our existing contact form made with the Contact Form 7 plugin. What do you say, there are some other options that are more simple? That’s right, but jQuery is very powerful and  flexible (choose your own theme / colors) so that’s why this combination is my favorite datepicker!

 

Related Posts

About Nico Julius

Nico is a teacher, developer and likes to share his ideas and vision on WordPressNinja.Com
  • http://highwiredesigns.net chris

    Thanks Nico!

    This is just what I’ve been looking for.

    • http://www.wordpressninja.com Nico Julius

      You’re welcome! :-)

  • http://www.sopov.com Vlad

    Thank you very much. It really works. +++

  • http://www.facebook.com/people/Tim-Smith/608676456 Tim Smith

    Thanks Nico, it’s excellent. Is there an easy way to add some of the options for datepicker? My dodgy syntax isn’t working.

    • http://www.wordpressninja.com Nico Julius

      Hi Tim, which options do you mean? Nico

    • Anonymous

      What options do you mean?
      Nico

  • Adam

    Just want to add the “id” must be set for the text field in Contact 7 for the above example to work. Thx for this Nico

    • Anonymous

      Hi Adam, thank you for letting me know!

  • Jonathan Warner

    Can’t figure out why this isn’t working for me. All jquery paths are fine. Adam, what do you mean ‘id’ must be set, set the same as here?:

    echo ‘$( “#start-date” ).datepicker({});’;

    I even removed the if statement to have it load on all pages, but it still does not appear.

    • Anonymous

      Jonathan you need to set the id in Contact 7 ex. Date

      [/text]
      • Anonymous

        thanx for your reply!

    • Anonymous

      Hi Jonathan, glad you found it. Sorry for the little mistake, I updated the tutorial. Nico

  • Anonymous

    Hi guys, thanx for all the comments. I’m very sorry for the little mistake!!! I wrote this tutorial at 1 or 2 AM, maybe next time I’ll write my technical tutorials in the afternoon ;-) . I edited the post, the code (id:start-date) should now be right. Very sorry again and thanks for letting me know. Nico

  • http://www.zen-garden.org/ Piet

    Will this also work on a dynamic field in CF7. My date fields are pre-filled with
    http://sevenspark.com/wordpress-plugins/contact-form-7-dynamic-text-extension

    The dates can however be modified and for that I would like the datepicker.
    Thanks,

  • http://www.vakantiehuisverhuur.eu/ Piet

    Great, could not wait so gave it a try with the pre-filled dynamic data fields and it works just fine.

    Rather then putting it in the header.php I have it included on my form-page where I also execite php (with the PHP-execution plugin). The CSS link I have put into the HEAD section of my theme.

    Thanks for your clear “how to”.
    Piet,

    • Anonymous

      Hi Piet,
      Glad you could use my tutorial! You’re right, putting the code in your form page is also a good solution.
      Kind regards,
      Nico

  • Phan Kimi

    Thanks for this tutorial. Very clear to follow.

  • Anonymous

    Very good tutorial. Thanks for it.

  • Moe

    Amazing tutorial, but is it safe to add an end-date into the code like this:
    echo ”;
    echo ‘jQuery(function($){‘;
    echo ‘$( “#start-date” ).datepicker({});’;
    echo ‘$( “#end-date” ).datepicker({});’;
    echo ‘});’;
    echo ”;

    • Anonymous

      Should be no problem.

      In your form you add

      [/text]
  • Sonofara

    very helpful tutorial, Thanks….but i need a little more help as i have 2 or more text fields where im getting the date picker working only for the very first field. How to get on the other fields…. i tried this way:

    [/text]
    [text 1="id:start-date" language="end-date"]

    Please suggest….Thanks again

    • Anonymous

      Hi
      Take a look at Moe’s comment and my answer on that. It should work like that. If not, let me know!

      • Sonofara

        Great! its working…….Thanks >>WordpressNinja ROCKS<<

  • http://www.realestatehomepark.com Rizky

    Hello
    Thanks for the guide adding jquery calender in CF 7
    i almost using different plugin but your guide trough me just using CF7 we can still using datepicker

    • Anonymous

      I’m glad I could help you!

  • Chris

    Thanks for the tutorial!

  • www.junkaria.com

    Thanks for this tutorial itz amazing:-) and are in very easy steps:-)

  • tinsterman

    Great tutorial, I can see how to implement 2 date fields by Moe’s comment. However, is there a way to prepopulate the 2nd Calendar field. For instance I have two date fields
    Arrival Date & Departure Date
    Can I force the Departure Date calendar to only show dates after the Arrival Date?

  • Bliz

    I want to add the datepicker to an input type field in the admin panel and not in a metabox. How can I do it. Thanks!

    • http://www.wordpressninja.com Nico Julius

      Hi Bliz,
      Do you mean in the admin panel of WordPress (back office)?
      Nico

  • Louisa

    Thanks for this. I have got this working, but I have a conflict with my banner images (which pull from custom fields)

    ID, ‘banner’, true)) : ?>
    <img src="ID, ‘banner’, true); ?>” />

    I am bit of a noob, when it comes to this stuff. Any help appreciated.

    • Louisa

      this > doesn’t appear in my code BTW

      • http://www.wordpressninja.com Nico Julius

        Can you send me the url of the web page? You can send it to me via the contact form if you want to.

    • Louisa

      sorry, it seems posting code has unformatted and deleted some of the code..

  • alex

    Thanks for this, but how would you go about using the Jquery files that are already part of wordpress?

    • http://www.wordpressninja.com Nico Julius

      Hi Alex, it depends of the files that are already part of WordPress. If the needed files are part of a plugin or something else, then you don’t have to insert the jQuery code again.

  • Webmagus

    what do i need to do if i am going to use this in multiple pages.

    if($post->ID==21){

    what if i need it for ID=201 or 202 or 203

    how do i make it work?

    thanks.

    • http://www.wordpressninja.com Nico Julius

      Hi Webmagus, this should work for you:
      if(($post->ID==201) OR ($post->ID==202) OR ($post->ID==203)){
      Nico

  • http://twitter.com/rsutaria R Sutaria

    Is there a way to make the date-picker start from a particular date? As in, if I wanted to restrict the user to select only dates starting from a particular date onwards (say from Jan 7th, 2012 onwards) – is it possible to do that using the above Contact Form 7 datepicker plugin?

  • http://twitter.com/ryanhebl Ryan Hebl

    Great Tutorial, This works perfectly for me.
    Thanks

  • Terrence

    I got everything working. But when I try my form and pick a certain date it’s not logging it on the email. What is the email field [start-date] ??

    • http://www.wordpressninja.com Nico Julius

      Hi Terrence, it should be just [start-date] like you. If it doesn’t work, just send me a mail with your code (form and e-mail content). Kind regards, Nico

      • Terrence

        Thanks for the fast reply. I figured that, it wasn’t working because I had watermark on the CF7 Field. Is there a way to implement watermark?

        • Terrence

          I figured out a work around the watermark. Instead of the usual watermark code CF7 comes with I just used placeholder.
          Like this:

          [/text]
          • http://www.facebook.com/matt.litherland Matt Litherland

            Nice one, thanks for the solution mate!

          • http://www.wordpressninja.com Nico Julius

            Hi Terrence, sorry for my late reply, I was very busy. Glad you found a solution, and looks like you also helped some other guys here. Thanks to post it here!

  • Andy

    Thank You Nico & Thank You Terrance… The watermark bug was a good one to find.. Would have wasted a lot of time on finding that out…

  • Shawn

    Is there a way to exclude certain days of the week? The business I am working on is closed on Wednesdays.