How to use jQuery in WordPress theme?

I’m try to use jQuery in 2011 WordPress theme I added this to the header:

wp_enqueue_script( 'jquery' );

and tested using this:

Read More
<script type="text/javascript">
    $(document).ready(function(){
        alert("test");
    });
</script>

I also used jQuery instead of $ but this did not work.

Please help, thanks

Related posts

Leave a Reply

3 comments

  1. I literally just had this issue on WordPress the other day. I solved it simply by adding my script to the header, and making certain that jQuery was running.

    Instead of writing your script directly in the header, it might be better to link to an external file (example):
    <script type="text/javascript" src="http://yourwebsite/javascripts/.js"></script>

    Host it somewhere on your server and then link to it – if it contains event handlers, they should bind without a problem.

  2. Put this code in the theme function.php:

    function custom_script() { ?>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                alert("test");
            });
        </script><?php
    }
    
    add_action('wp_head', 'custom_script');
    
  3.   <?php wp_enqueue_script("jquery"); ?>
    
      <?php wp_head(); ?>
    

    Put this code inside ( section) of your header.php file of your theme.

    Now you can call

          <script type="text/javascript"
              src="<?php bloginfo("template_url"); ?>/js/yourScript.js">
          </script>
    

    In this way you are ready to use jquery into your theme.