4

i want to add a datepicker on a custom page but i doesn not work. WP version is 3.2.1. Those are the init string i used on wp code:

wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-datepicker', get_bloginfo('template_url') . '/js/jquery-ui-datepicker.min.js', array('jquery','jquery-ui-core'));
wp_enqueue_style('jquery.ui.theme', get_bloginfo('template_url') . '/js/smoothness/jquery-ui-1.8.16.custom.css');

Those enqueques generate this code:

<script type='text/javascript' src='http://www.xxxxxxxx.it/wp-includes/js/jquery/jquery.js?ver=1.6.1'></script>
<script type='text/javascript' src='http://www.xxxxxxxx.it/wp-includes/js/jquery/ui.core.js?ver=1.8.12'></script>
<script type='text/javascript' src='http://www.xxxxxxxx.it/wp-content/themes/greyzed/js/jquery-ui-datepicker.min.js?ver=3.2.1'></script>
<link rel='stylesheet' id='jquery.ui.theme-css'  href='http://www.xxxxxxxx.it/wp-content/themes/greyzed/js/smoothness/jquery-ui-1.8.16.custom.css?ver=3.2.1' type='text/css' media='all' />

On the page body, i use this code:

<script type="text/javascript">
    jQuery('#datepicker').datepicker({
        dateFormat : 'yy-mm-dd'
    });
</script>
<div class="row"><label for="day">Data</label><input type="text" id="datepicker" name="day" class="text" /></div>

The datepicker does not work. I do not know how to debug this issue. It does nothing, like the jQuery is not even there.

UPDATE:

With your help i managed to debug the code. I placed the script after the div and changed into:

<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('#datepicker').datepicker({
        dateFormat : 'yy-mm-dd'
    });
});
</script>

This code give me error when calling the datepicker method, the error states:

Uncaught TypeError: Object [object Object] has no method 'datepicker'

If i type jQuery('#datepicker') on javascript console, i get this:

[<input type=​"text" id=​"datepicker" name=​"day" class=​"text">​]

I get no other errors except for that one, all the references to jQuery seems to work fine.

UPDATE 2:

Finally i got it working! i had to place 'wp_print_scripts' instead of 'init', and i had to reposition some init code of another plugin that was conflicting. The only remaining issue are the themes... if i use the basic theme within googlecode, it works. If i use other themes (embedded in wp or linked with wp_enqueque_style) the theme will be not loaded.... if i check the generated html, there is no sign of the line that should load the jQuery theme.

6
  • This seems fine on WP level (proper enqueues, output, non-shorthand jQUery call) and as such is probably not WordPress-specific. Any more WP-related details? Otherwise best asked on StackOverflow.
    – Rarst
    Commented Oct 30, 2011 at 21:22
  • If your "custom page" is an admin page, then you need to enqueue it a little differently. See: codex.wordpress.org/Plugin_API/Action_Reference/…
    – Tom Auger
    Commented Oct 30, 2011 at 22:24
  • What other wp details do you need? Ask and i will answer. the page is not Admin specific. It is just a wp page, made using a crafted page template that contains the code i'm using.
    – Terix
    Commented Oct 31, 2011 at 7:16
  • Also, just because the enqueue has put the necessary SCRIPT or STYLE tags into your head, doesn't necessarily mean that it has actually loaded (the URLs could be wrong - happened to me a dozen times!). Again, use Firebug or Google Chrome and check your network statistics - if you see red 404 errors, that means that the script or CSS file isn't being found at the address specified.
    – Tom Auger
    Commented Oct 31, 2011 at 12:58
  • script and styles are all found,i get the only error far below, on the actual call to method datepicker()
    – Terix
    Commented Oct 31, 2011 at 20:57

4 Answers 4

4

I often type things wrong. So, I would start debugging you copying and pasting the links to the JS scripts in your browser and make sure they load.

Then in Chrome go to the Wrench Menu -> Tools -> JavaScript Console. Here you will be able to type/execute your JavaScript directly. I would start off my typing jQuery into the console to make sure jQuery is actually loaded. Then try doing jQuery('#datepicker') if it returns empty brackets [] then your selector is failing. If it works, try opening up the datepicker - you will probably see an error in the JS console.

Like others have suggested, I think the problem is that the script is running before the is actually rendered. I would suggest doing the following:

<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('#datepicker').datepicker({
        dateFormat : 'yy-mm-dd'
    });
});
</script>

This will force the script to run after the entire page has loaded.

== EXAMPLE ==

add_action( 'init', 'wp29r01_date_picker' );
function wp29r01_date_picker() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'jquery-ui-core' );
    wp_enqueue_script( 'jquery-datepicker', 'http://jquery-ui.googlecode.com/svn/trunk/ui/jquery.ui.datepicker.js', array('jquery', 'jquery-ui-core' ) );
}

add_action( 'wp_footer', 'wp29r01_print_scripts');
function wp29r01_print_scripts() {
    ?>
<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('#datepicker').datepicker();
    })
</script>
    <?php
}
5
  • I've followed you advices, and i made some steps: now i have the error that is stopping my code from working. It say: Uncaught TypeError: Object [object Object] has no method 'datepicker' on the line jQuery('#datepicker').datepicker
    – Terix
    Commented Oct 31, 2011 at 20:33
  • If thats the case, then the datepicker script is not being included properly, or is the wrong script. I'll update my post with code that works.
    – v0idless
    Commented Oct 31, 2011 at 21:14
  • This is driving me crazy.... i still have the same error! I changed the code according to your suggestion, when i write jQuery('#datepicker') i get the div like before, but still i get also the same exact error! I've tested with an empty html file, putting the html output of the wp_enqueques, and with this test page it works, so there is something inside wp that is screwing up datepicker(), but i have no idea on how to solve this...
    – Terix
    Commented Nov 1, 2011 at 7:34
  • Then what I would do is set your theme to twentyeleven, and deactivate all other plugins. Turn them on one by one until you find which one is causing datepicker to break.
    – v0idless
    Commented Nov 1, 2011 at 12:32
  • Finally i got it working! i had to place 'wp_print_scripts' instead of 'init', and i had to reposition some init code of another plugin that was conflicting. The only remaining issue are the themes... if i use the basic theme within googlecode, it works. If i use other themes (embedded or linked with wp_enqueque_style) the theme will be not loaded.
    – Terix
    Commented Nov 2, 2011 at 7:20
3

For those who debug "not working" datepicker - for me it was an issue of my reset css, specifically of this:

html, body { overflow: auto; }

My datepicker was alright, but kept appearing on far top of the screen. :)

0

In your example, the jQuery is placed before the 'datepicker' div is created. So it won't do anything. Either place the script block after the 'row' div, or use jQuery(document).ready() instead.

2
  • I placed the script after the '<div>' but nothing changed. I have made a test page, plain html, with only the html generated from the wp page, it still does not work, how can i debug it?
    – Terix
    Commented Oct 31, 2011 at 7:22
  • Make sure you have Firebug, google Chrome, or use Explorer with Developer Tools turned on (F12). You want to check the error console and look for JavaScript errors. Feel free to post a link and we'll take a look.
    – Tom Auger
    Commented Oct 31, 2011 at 12:56
0

Include your custom script also via wp_enqueue_script() and reference the other libs and you dont have no problems with the timeline; your custom script to ask for the libs of datepicker load after all other scripts is ready; works fine and also in feature releases of WP.

2
  • do you mean to put the jquery code inside a js file and call it?
    – Terix
    Commented Oct 31, 2011 at 7:23
  • @bueltge that would only solve the problem if Terix called it like this: wp_enqueue_script( 'do-date', 'http://www.xxxxxxxx.it/wp-content/themes/greyzed/js/do_date.js' , array('jquery-ui-datepicker'), 1, true ) - the last 'true' being important so that it puts the script in the footer, namely after the div has been created. I still prefer using document.ready(), regardless.
    – Tom Auger
    Commented Oct 31, 2011 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.