plugin development - Correct way to enqueue jquery-ui

I'm having a tough time including jquery-ui scripts and styles in my plugin. It seems that my wp_enqueue_script cal

I'm having a tough time including jquery-ui scripts and styles in my plugin. It seems that my wp_enqueue_script calls are simply ignored.

There are already many questions similar to this one, but all answers I've found so far boil down to call wp_enqueue_script inside the wp_enqueue_scripts action hook, which I'm already doing.

In the constructor of my class I call:

add_action( 'wp_enqueue_scripts', array($this, 'enqueue_scripts') );

and then, below:

public function enqueue_scripts()
{    
  wp_enqueue_script( 'jquery-ui-core', false, array('jquery') );
  wp_enqueue_script( 'jquery-ui-widget', false, array('jquery') );
  wp_enqueue_script( 'jquery-ui-mouse', false, array('jquery') );
  wp_enqueue_script( 'jquery-ui-accordion', false, array('jquery') );
  wp_enqueue_script( 'jquery-ui-autocomplete', false, array('jquery'));
  wp_enqueue_script( 'jquery-ui-slider', false, array('jquery'));

I've checked that the code actually gets executed each page load. However the pages lack the <link> tags for the jquery-ui library. I've already tried with and without the jquery dependency explicitly specified in the third argument of the wp_enqueue_script calls.

I've also tried with a plain WP 4.8 installation with no plugins installed other than mine, and with the default twenty seventeen theme only. No dice.

What's wrong with my code?

I'm having a tough time including jquery-ui scripts and styles in my plugin. It seems that my wp_enqueue_script calls are simply ignored.

There are already many questions similar to this one, but all answers I've found so far boil down to call wp_enqueue_script inside the wp_enqueue_scripts action hook, which I'm already doing.

In the constructor of my class I call:

add_action( 'wp_enqueue_scripts', array($this, 'enqueue_scripts') );

and then, below:

public function enqueue_scripts()
{    
  wp_enqueue_script( 'jquery-ui-core', false, array('jquery') );
  wp_enqueue_script( 'jquery-ui-widget', false, array('jquery') );
  wp_enqueue_script( 'jquery-ui-mouse', false, array('jquery') );
  wp_enqueue_script( 'jquery-ui-accordion', false, array('jquery') );
  wp_enqueue_script( 'jquery-ui-autocomplete', false, array('jquery'));
  wp_enqueue_script( 'jquery-ui-slider', false, array('jquery'));

I've checked that the code actually gets executed each page load. However the pages lack the <link> tags for the jquery-ui library. I've already tried with and without the jquery dependency explicitly specified in the third argument of the wp_enqueue_script calls.

I've also tried with a plain WP 4.8 installation with no plugins installed other than mine, and with the default twenty seventeen theme only. No dice.

What's wrong with my code?

Share Improve this question edited Feb 25, 2020 at 12:31 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Jul 20, 2017 at 8:46 Lucio CruscaLucio Crusca 6883 gold badges9 silver badges29 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 40

First of all, WordPress registers jQuery UI via wp_default_scripts(). Dependencies are already set, so you only need to enqueue the script you really need (and not the core). Since you're not changing version number or anything, it is ok to only use the handle.

// no need to enqueue -core, because dependancies are set
wp_enqueue_script( 'jquery-ui-widget' );
wp_enqueue_script( 'jquery-ui-mouse' );
wp_enqueue_script( 'jquery-ui-accordion' );
wp_enqueue_script( 'jquery-ui-autocomplete' );
wp_enqueue_script( 'jquery-ui-slider' );

As for the stylesheets: WordPress does not register jQuery UI styles by default!

In the comments, butlerblog pointed out that according to the Plugin Guidelines

Executing outside code within a plugin when not acting as a service is not allowed, for example:

  • Calling third party CDNs for reasons other than font inclusions; all non-service related JavaScript and CSS must be included locally

This means loading the styles via CDN is not permitted and should always be done locally. You can do so by using wp_enqueue_style().

If you are enqueuing your own script, you can just add 'jquery-ui-accordion', for example, to the list of dependencies. All the required dependencies will be added automatically. Example:

wp_enqueue_script( 'my-theme', get_stylesheet_directory_uri() . '/js/theme.js', array( 'jquery', 'jquery-ui-accordion' ) );

Will generate this code:

<script type='text/javascript' src='/wp-includes/js/jquery/ui/core.min.js'></script>
<script type='text/javascript' src='/wp-includes/js/jquery/ui/widget.min.js'></script>
<script type='text/javascript' src='/wp-includes/js/jquery/ui/accordion.min.js'></script>
<script type='text/javascript' src='/wp-content/themes/theme/js/theme.js'></script>

I've modified your script. try with this, it is working.

add_action( 'wp_enqueue_scripts', array($this, 'enqueue_scripts') );
public function enqueue_scripts()
{    
  wp_enqueue_script( 'jquery-ui-core');
  wp_enqueue_script( 'jquery-ui-widget');
  wp_enqueue_script( 'jquery-ui-mouse');
  wp_enqueue_script( 'jquery-ui-accordion' );
  wp_enqueue_script( 'jquery-ui-autocomplete');
  wp_enqueue_script( 'jquery-ui-slider');
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744716395a4589644.html

相关推荐

  • plugin development - Correct way to enqueue jquery-ui

    I'm having a tough time including jquery-ui scripts and styles in my plugin. It seems that my wp_enqueue_script cal

    12天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信