Ask your WordPress questions! Pay money and get answers fast! (more info)

Adding Jquery into Wordpress in Correct manner. WordPress

  • REFUNDED

Hi 2 all,

I am using locality Real Estate wordpress theme .When I added Jquery in My Footer.php File, My Site got Issues. Slideshow, drop down and some css have been changed. But the Property Search Working without any issue. I thing something wrong with integeration of jquery . Can anyone please help me to put the jquery in correct manner in My Theme File. This is My [[LINK href="http://www.excelproperty.lk/property/"]]Site[[/LINK]]. If you need wordpress admin details I can send it to you. Please post your skypeid. The Following is the Code Which I have Added.

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js">
</script>
<script type="text/javascript">
$(window).load(function(){
var maxprice = $('#select-maxprice').html();
$('#select-minprice').change(function() {
$('#select-maxprice').html(maxprice);
$('#select-maxprice option').each(function() {
if (parseInt($(this).val()) <= parseInt($('#select-minprice').val())) $(this).remove();
});
});
})
</script>




Answers (6)

2012-12-27

phppoet answers:

To avoid conflict you should use load jquery using wp_enqueue_script

check this .

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

you can add your scripts in this way

<?php
function my_scripts_method() {
wp_enqueue_script(
'custom-script',
get_template_directory_uri() . '/js/custom_script.js',
array('jquery')
);
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
?>


and then load scripts using this way


function my_init() {
if (!is_admin()) {
wp_enqueue_script('jquery');
}
}
add_action('init', 'my_init');

2012-12-27

Dylan Kuhn answers:

Your site is already loading a different version of jQuery. Try your snippet without the initial script tag:


<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js">
</script>

2012-12-27

Arnav Joy answers:

try this in functions.php

function my_init() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js');
wp_enqueue_script('jquery');

?>
<script type="text/javascript">

$(window).load(function(){

var maxprice = $('#select-maxprice').html();

$('#select-minprice').change(function() {

$('#select-maxprice').html(maxprice);

$('#select-maxprice option').each(function() {

if (parseInt($(this).val()) <= parseInt($('#select-minprice').val())) $(this).remove();

});

});

})

</script>
<?php
}
}
add_action('init', 'my_init');

2012-12-28

Kiet Luong answers:

First you load jQuery follow this code in your functions.php file:
function my_init() {

if (!is_admin()) {

wp_enqueue_script('jquery');

}

}

add_action('init', 'my_init');


Then add your custom jQuery scripts in footer.php



<script type="text/javascript">

(function($){

$(window).load(function(){

var maxprice = $('#select-maxprice').html();

$('#select-minprice').change(function() {

$('#select-maxprice').html(maxprice);

$('#select-maxprice option').each(function() {

if (parseInt($(this).val()) <= parseInt($('#select-minprice').val())) $(this).remove();

});

});

})

})(jQuery);

</script>


If it still not work then try to load jQuery again with this code:



function my_init() {

if (!is_admin()) {

wp_deregister_script('jquery');

wp_register_script('jquery', 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js');

wp_enqueue_script('jquery');

}
}
add_action('init', 'my_init');


in functions.php


Kiet Luong comments:


<script type="text/javascript">



<strong><em>(function($){</em></strong>



$(window).load(function(){



var maxprice = $('#select-maxprice').html();



$('#select-minprice').change(function() {



$('#select-maxprice').html(maxprice);



$('#select-maxprice option').each(function() {



if (parseInt($(this).val()) <= parseInt($('#select-minprice').val())) $(this).remove();



});



});



})



<strong><em>})(jQuery);</em></strong>



</script>

2012-12-28

Ellah Alzona answers:

Try un installing the backgroundposition plugin if you have then insert your javascript in the footer again.

2012-12-28

Francisco Javier Carazo Gil answers:

You could load your code directly here:

wp_enqueue_script(
"my-script"
,path-to-your-script
,array('jquery')
,0.1 // here always increment between one test and the next
,true // to load in FOOTER this is really important
);

Remember the last two parameters.