Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know that wp_enqueue_script() inserts Javascript files into the WordPress header in the appropriate place.

However, I want to include my Javascript in all single posts, ignoring the WordPress home or blog page.
Is that possible at all?

I tried this:

<?php
function my_scripts_method() {
    if ( is_single() ) {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
        wp_enqueue_script( 'jquery' );
    }
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

But it's not working...

share|improve this question
add comment

1 Answer

if you want it on all single posts as well as pages & custom post types, use is_singular() instead of is_single()

& just in case you're using it on the template, this code goes in functions.php

UPDATE IN RESPONSE TO THE COMMENTS

write this code just above your wp_head() function in header.php

global $wp_scripts;
var_dump($wp_scripts);

This will display on the site all the info related to scripts registered in wordpress to be displayed. Read it & then see what scripts you want to display & what scripts are to be removed with wp_dequeue_script

To determine which page you are removing scripts from, you may look for various Conditional Tags

share|improve this answer
    
My Wordpress Home Page is too heavy with jquery, javascript bla bla. i'm trying to remove javascript who not necessary on home page. so i want to implement on my wordpress template. Please let me know, some trick to filter javasript on wordpress home page. –  user1218948 Oct 19 '12 at 14:52
    
All these javascript come form wordpress plugins –  user1218948 Oct 19 '12 at 14:53
    
This would have to be done on a plugin-by-plugin basis then as they most likely would all have unique script enqueue names. Also, you may risk having JavaScript errors popup on your home page if you just disable the scripts without disabling the plugins. –  hereswhatidid Oct 19 '12 at 15:43
    
so how i implement on plugin? i tested is_single() its not working –  user1218948 Oct 19 '12 at 16:48
1  
Unfortunately not all plugins follow the rules when it comes to enqueueing scripts. There's a very good chance that between plugins and themes you'll have at least one instance where something is directly echoing a call for script inclusion. –  hereswhatidid Oct 19 '12 at 18:57
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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