0

I am attempting to implement a jQuery UI dialog in Wordpress 4.5.3, but it only works about half the time. The rest of the time I get the following error:

Uncaught TypeError: jQuery(...).dialog is not a function  lwr_jquery_dialog.js(2)

I have enqueued the scripts as follows, and verified they load in the correct order on the page:

function enqueue_jquery_dialog() {

    wp_enqueue_script('jquery-ui-dialog', false, array('jquery','jquery-ui-core'), 
        false, true );
    wp_enqueue_script('lwr_dialog', get_stylesheet_directory_uri() . 
        '/js/lwr_jquery_dialog.js', array('jquery-ui-dialog') );        
}
add_action( 'wp_enqueue_scripts', 'enqueue_jquery_dialog' );

In lwr_jquery_dialog.js I have:

jQuery(document).ready(function() {
    jQuery('#dialog').dialog({
        width: 600,
        modal: true,
        resizable: false,
        draggable: false,
    });
});

And here's the actual dialog function in functions.php

function add_jquery_dialog() {
    ?>  
    <div id="dialog" title="Today Only">
            DIALOG TEXT HERE
    </div>
<?php
}
add_action('wp_footer', 'add_jquery_dialog');
3
  • Half the time sounds like a script is not in the right order, and just so happens to either be cached the second time or loads just-in-time that you don't notice it being an issue the first time. Check the order of your scripts and ensure jquery-UI is loaded after the main jquery library. if it's not that, stay open-minded about it when you look at the code again - if it happens to be something like this let me know and I'll make an answer of it! Commented Aug 7, 2016 at 1:21
  • Have you tried changing the priority of the add_action? Commented Aug 7, 2016 at 1:24
  • Try changing jQuery to $ Commented Aug 8, 2016 at 7:17

1 Answer 1

2
+50

Update 1: You're missing jquery-ui-dialog script. Try this instead:

add_action('wp_enqueue_scripts', function()
{
  $theme_uri = get_stylesheet_directory_uri();

  wp_enqueue_script('lwr-jquery-dialog', $theme_uri . '/js/lwr_jquery_dialog.js',
['jquery-ui-core', 'jquery-ui-dialog'], false, true);
}, 10, 0);

Since you're using false as value of source file, the dependencies are not enqueued correctly. See $src param from wp_enqueue_script() function for more info.

Try this:

add_action('wp_enqueue_scripts', function()
{
  $theme_uri = get_stylesheet_directory_uri();

  wp_enqueue_script('lwr-jquery-dialog', $theme_uri . '/js/lwr_jquery_dialog.js',
['jquery-core', 'jquery-ui-core'], false, true);
}, 10, 0);
Sign up to request clarification or add additional context in comments.

3 Comments

Even when it fails, I've verified in the html source that the files are loading in the correct order. I tried this, but with no additional luck.
@dpruth I have made a test and updated the answer. Hope it works this time.
My script above includes jquery-ui-dialog. But I did update the wp_enqueue_script function with your suggestion (taking out "FALSE") and it seems to be working now. Thanks!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.