4

I've read the codex section on wp-enqeue, but am still struggling.

Basically, I would like to get the following to display properly in my theme's widget area (on every page):

<link rel="stylesheet" type="text/css" href="/wp-content/uploads/social_counter/css/styles.css" />
<link rel="stylesheet" type="text/css" href="/wp-content/uploads/social_counter/css/tipTip.css" />

<div id="social_counter">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="/wp-content/uploads/social_counter/js/jquery.tipTip.minified.js"></script>
<script type="text/javascript" src="/wp-content/uploads/social_counter/js/social_counter.js"></script>
</div>

I created a text widget into which I pasted the code, but it breaks a few things in the template as there is likely a conflict since jquery is already loaded by the theme and/ or other plugins.

Could someone show me how to best form this using wp_enqueue, and where to paste the subsequent code (functions.php of theme?)?

Thanks.

I've tried the following:

if(!is_admin()){
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
wp_enqueue_script('custom_script',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/jquery.tipTip.minified.js',false);
wp_enqueue_script('custom_script',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/social_counter.js',array('jquery'),'1.4.2',false);
}

It seem to load two of the scripts when I view the page source, but not the google jquery, and the content does not show...

now i have the following, still nothing (two scripts show up, but not the google jquery):

wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
wp_enqueue_script( 'jquery' );
wp_enqueue_script('custom_script',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/jquery.tipTip.minified.js',false);
wp_enqueue_script('custom_script',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/social_counter.js',array('jquery'),'1.4.2',false);

Latest Edit.. (i really wish stack overflow would adopt a mroe user-friendly threading system :/ )

function add_scripts(){
// Load jQuery
if ( !is_admin() ) {
   wp_deregister_script('jquery');
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"), false);
   wp_enqueue_script('jquery');
}
// Your Scripts
wp_enqueue_script('custom_script',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/jquery.tipTip.minified.js',false);
wp_enqueue_script('custom_script',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/social_counter.js',array('jquery'),'1.4.2',false);
}
add_action('init','add_scripts');
2
  • Is it worked???
    – Dijo David
    Commented Mar 24, 2011 at 6:17
  • no, for some reason, i'm sure i'm messing up. it does seem to laod the scripts, but the actual content does not appear.. Commented Mar 26, 2011 at 16:20

3 Answers 3

1

You actually don't need to worry about conflicting with the admin pages anymore. There is a "wp_enqueue_scripts" hook that makes sure the scripts aren't called on admin pages.

From WP Codex:

<?php
function my_scripts_method() {
  wp_deregister_script( 'jquery' );
  wp_register_script( 'jquery',    'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
  wp_enqueue_script( 'jquery' );
}    

add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

But if you need a custom jQuery core (or add-on) for the admin pages, you'll need to use the "init" hook with the !admin conditional.

3

Have a look at this link:

5-tips-for-using-jquery-with-wordpress

You can add the scripts by calling a function in functions.php.

Here is an example how to do this:


function add_scripts(){
        wp_register_script('scriptName', 'Path to the script'); //Register script
        wp_enqueue_script('jquery'); 
        wp_enqueue_script('scriptName'); //adding script

    }
    add_action('init','add_scripts'); //calling function in init

Also this link will be helpful.

EDIT:

Try this one

function add_scripts(){
// Load jQuery
if ( !is_admin() ) {
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"), false);
   wp_enqueue_script('jquery');
}
// Your Scripts
wp_register_script('script1', get_bloginfo('url').'/wp-content/uploads/social_counter/js/jquery.tipTip.minified.js');
wp_register_script('script2',get_bloginfo('url').'/wp-content/uploads/social_counter/js/social_counter.js',array('jquery'),'1.4.2',false);
wp_enqueue_script('script1');
wp_enqueue_script('script2');
}
add_action('init','add_scripts');
7
  • @Dijo David please see my revisions Commented Mar 22, 2011 at 4:55
  • @vulgarbulgar you have not used wp_enqueue_script('jquery');
    – Dijo David
    Commented Mar 22, 2011 at 5:02
  • @vulgarbulgar How u calling the function in functions.php? I have edited and add 1 more ink for reference.
    – Dijo David
    Commented Mar 22, 2011 at 5:08
  • @Dijo David Edited. see revisions above Commented Mar 22, 2011 at 5:11
  • @vulgarbar is_admin() is checking whether the current page is admin panel or dashboard. So there will be no conflict when you are in admin panel. How u calling this code? Put all the code inside a function and call the function in init. Its worked for me..
    – Dijo David
    Commented Mar 22, 2011 at 5:27
1

Best place is in your functions.php.

Example:

wp_enqueue_script("name", ("path/to/file"), false);

The false array will enqueue the script into the header, where you define wp_header(); - If you set it to true, it will enqueue the script into your footer where you define wp_footer();

Edit:

wp_enqueue_script('yourscriptname', get_bloginfo('stylesheet_directory') . '/js/jsyourscript.js');

In your revised post you have this code

if(!is_admin()){
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
wp_enqueue_script('custom_script',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/jquery.tipTip.minified.js',false);
wp_enqueue_script('custom_script',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/social_counter.js',array('jquery'),'1.4.2',false);
}

Please notice that you've placed the 2 scripts you wish to enqueue inside the conditional statement that if not admin - do this -

This may be the reason it's not appearing to work for you.

Edit 2

// Load jQuery
if ( !is_admin() ) {
   wp_deregister_script('jquery');
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"), false);
   wp_enqueue_script('jquery');
}
// Your Scripts
wp_enqueue_script('custom_script',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/jquery.tipTip.minified.js',false);
wp_enqueue_script('custom_script2',get_bloginfo('wpurl').'/wp-content/uploads/social_counter/js/social_counter.js',array('jquery'),'1.4.2',false);

Cleaned it up a little, put the jquery back into the conditional statement also put false onto it.

Also each new script you enqueue has to have a unique name. Both of yours are called custom_script

4
  • @vulgarbulgar Hmm, I'm not sure what supposedly wrong right now, nothing in the Edit 2 is wrong. What's currently not working?
    – tfbox
    Commented Mar 22, 2011 at 5:41
  • @tfbox demo.tutorialzine.com/2010/05/… - just trying to include this essentially, inside a widget area. testing on a page for now until i get it working at all. Commented Mar 22, 2011 at 5:43
  • @vulgarbulgar - I like it, looks good. So what's not working currently via wordpress?
    – tfbox
    Commented Mar 22, 2011 at 5:54
  • @tfbox haha, thats not mine- thats the demo version :) i would like that to work in my wp Commented Mar 22, 2011 at 6:07

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.