0

I really want to run a simple code in my template file.

E.g. if I put somethign like this in my page.php:

<script type="text/javascript">
    jQuery(document).ready(function($) {
        alert("Hello world!");
    });
</script>

It's not working.

But simple javascript is working:

<script type="text/javascript">
        alert("Hello world!");
</script>

Why is jQuery blocked in WP to be included in template files?

and maybe more important question

WHERE IN THE WP SETTINGS CAN I RE-ENABLE THIS?

I know I can use wp_register_script etc. but in my scenario I need to paste simple javascript code in the template files.

How to allow load jQuery code snippets in WP template's file too?

And why is it even blocked?

-2

I don't think jQuery script is en-queued by default with WordPress templates. So you should place the jQuery script in the header/footer for your code snippet to work.

9
  • Could you explain what is preventing the code from working? Is there some filter that is removing jquery from the template? How can I turn this filter off in WP e.g. could that be done in funcitons.php? – Derfder Oct 2 '13 at 10:34
  • My understanding is that you haven't enqueued the jquery script to work with templates. Have you? You can check this by seeing the source code of the header from any browser. You should see a jQuery script in the header. If it is not there then you must en-queue it either in the functions or in the template file for your snippet to work. *By jQuery script i mean the jQuery script not your jQuery code. – sri Oct 2 '13 at 10:42
  • Also there are no filters to remove your jquery code. – sri Oct 2 '13 at 10:44
  • E.g. I have my own CMS and I can put the script everywhere and it will run. If the jquery is loaded in the header, every jquery script will run. Therefore something in the WP code is preventig that for some reason. – Derfder Oct 2 '13 at 11:19
  • 1
    Actually, you should enqueue the script, not add it directly to the template header/footer. – Chip Bennett Oct 2 '13 at 11:48
5

WordPress does not automatically enqueue jQuery in the template (front end). The Theme must do so.

The correct way to enqueue jQuery in the template is to add a callback to wp_enqueue_scripts, like so:

function wpse116473_enqueue_scripts() {
    if ( ! is_admin() ) {
        wp_enqueue_script( 'jquery' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse116473_enqueue_scripts' );

Since the wp_enqueue_scripts action gets fired via the wp_head action, which is called by the wp_head() template tag (generally found in header.php), the callback will be executed in all contexts in the template. If, for example, you only want to enqueue a script in your particular page template, you have a couple of options:

  1. Use a conditional wrapper in the above callback, e.g.:

    function wpse116473_enqueue_scripts() {
        if ( ! is_admin() ) {
            if ( 'template-foobar.php' == get_page_template() ) {
                wp_enqueue_script( 'jquery' );
            }
        }
    }
    add_action( 'wp_enqueue_scripts', 'wpse116473_enqueue_scripts' );
    
  2. Just call wp_enqueue_script() directly in the template, before you output your script, e.g.:

    wp_enqueue_script( 'jquery' );
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            alert("Hello world!");
        });
    </script>
    

    Since WordPress 3.4 (IIRC), scripts can be enqueued inline, which makes targeting the contexts in which they are enqueued much easier. (This is a great method for, e.g., a slider template-part file.)

-4

You could add jQuery in using this in your functions.php file:

function my_scripts_method() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://code.jquery.com/jquery-latest.js');
    wp_enqueue_script( 'jquery' );
}    

add_action('wp_enqueue_scripts', 'my_scripts_method');
7
  • 3
    Never deregister core-bundled scripts and register some different version of that script, especially a critical one such as jQuery. Core, Theme, and Plugins all rely on a known version of core-bundled scripts; replacing the core-bundled version can, will, and usually does lead to breakage. – Chip Bennett Oct 2 '13 at 13:13
  • 2
    @ChipBennett you sound incredible totalitarian and I guess you haven't lived in a communist country. Well I did and I must say. Stop to use the word "NEVER". Thank you. Btw. I am not asking for a "correct" way. I was asking for a way. – Derfder Oct 2 '13 at 13:33
  • 5
    @Derfder Questions & answers (& comments) are there to serve the community as a whole. While you may consider this answer to be suitable for you, it's wholly right and good for the site that its flagged as not being 'correct'. Chip was just expounding an important principle that is often missed (as demonstrated by this answer), and when not adhered to can cause users and developers a lot of problems. – Stephen Harris Oct 2 '13 at 13:57
  • 1
    @Derfder I'm happy to talk about this in chat if you want to discuss it further, but I would say you're right that the community can decide on its own. I would add that it has decided. Chip is also part of this community. As are the voters, comment-ers and yourself. – Stephen Harris Oct 2 '13 at 15:11
  • 2
    Aside from that, above added script would only work via http protocol scheme... facepalm You can avoid the scheme as a whole since IE4 IIRC. – kaiser Oct 2 '13 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.