trying to enqueue script in wordpress

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="https://wordpress.stackexchange.com/wp-content/uploads/social_counter/css/styles.css" />
<link rel="stylesheet" type="text/css" href="http://wordpress.stackexchange.com/wp-content/uploads/social_counter/css/tipTip.css" />

<div id="social_counter">
<script type="text/javascript" src="https://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');

3 Answers
3

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.

Leave a Comment