Im trying to implement some jquery to a wp plugin I am creating.
In the functions.php of my theme I have the following code:
function load_jquery() {
// only use this method is we're not in wp-admin
if ( !is_admin() ) {
// deregister the original version of jQuery
wp_deregister_script('jquery');
wp_deregister_script('jquery-ui-slider');
wp_deregister_style('jquery-ui-style');
// discover the correct protocol to use
$protocol='http:';
if($_SERVER['HTTPS']=='on') {
$protocol='https:';
}
// register the Google CDN version
wp_register_script('jquery', $protocol.'//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', false, '1.10.2');
wp_register_script('jquery-ui-slider', $protocol.'//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', false, '1.10.3');
wp_register_style('jquery-ui-style', $protocol.'//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css', false );
// add it back into the queue
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-slider');
wp_enqueue_style('jquery-ui-style');
}
}
add_action('wp_enqueue_scripts', 'load_jquery');
Then in one of the plugin files I output the following via a function and add_action('wp_head', 'functions_name');
echo '
<script type="text/javascript">
jQuery(function($) {
$( "#slider-range" ).slider({
range: true,
min: 0,
max: 500,
values: [ 75, 300 ],
slide: function( event, ui ) {
$( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
}
});
$( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
" - $" + $( "#slider-range" ).slider( "values", 1 ) );
});
</script>';
Still nothing works, even though I can see the jquery loaded before the actual script.
Any ideas? It works if I skip the wp_enqueue_script part and past everything inside the header.php of the themefile.
The big problem is the wordpress conflict situation in order for your script to work you need to replace all '$' signs you use in your jQuery with ' jQuery ' . Every good wordpress theme dev knows this , wordpress conflicts with your code so you need to adapt .
Example :
I hope i m right because i had the same issue , did that and it was solved . :)
You should wrap your script with
jQuery(document).ready(function($)
because the jquery object may be loaded but you need the jquery-ui scripts as well.