How to setup jQuery cookie plugin in wordpress?

2019-09-06 17:02发布

问题:

Is the same as installing any other wordpress plugins?

回答1:

I'd supplement saibot's answer by saying that, instead of a raw <script>...</script> block, you should use wp_enqueue_script() also when including the plugin script:

<?php wp_enqueue_script("jquery-cookie", get_bloginfo('template_url').'/jquery.cookie.js', array(), '0'); ?>


回答2:

Not really, you kind of have to go through the back door a little.

First off, make sure you're including jQuery the 'right' way (WP includes a copy):

 <?php wp_enqueue_script("jquery"); ?>

In the header, include the plugin script the way you normally would, eg,

 <script type='text/javascript' src='source/script.js'></script>

And then when you are writing your script, make sure it's in no-conflict mode:

 (function($){
    $(document).ready(function(){
       //Scripts go in here!
    });
 })(jQuery);

And then stick the actual script inside the jQuery no-conflict wrapper.

Hope this helps.



回答3:

And to make sure you're including jQuery, i'd supplement to Jani's answer:

<?php wp_enqueue_script("jquery-cookie", get_stylesheet_directory_uri().'/path/to/jquery.cookie.js', array( 'jquery' ), '0'); ?>

get_stylesheet_directory_uri() would be just in case your script is located in a child-theme

Old thread i know, just wanted to share as it got me stuck for some time... until i added that 'jquery' in the array.