0

I have this code to enqueue a script file when the page is not single.php (neither admin page):

//functions.php
/********************** Add JS file **************************/
function my_own_scripts() {
    if (! is_admin() && ! is_single()) {
        wp_register_script( 'my_javascript_file',get_bloginfo('stylesheet_directory') . '/js/my_javascript_file.js', array( 'jquery' ) );
        wp_enqueue_script( 'my_javascript_file' );
    }
}

add_action( 'wp_enqueue_scripts', 'my_own_scripts' );

But the script is always enqueued. Also in a single post entry.

I've read this post, but doesn't help me. Of course, the theme is using single.php and is_single() works with other functions.

Any idea?

Edit: I've found out that is_single() is always returning false, even when single.php is loaded. I can't see why this function is not working as expected while it is working right inside all other functions inside functions.php.

1
  • Don't use get_bloginfo('stylesheet_directory'). You should be using get_stylesheet_directory_uri(). Commented Nov 6, 2014 at 13:19

2 Answers 2

1

With a couple minor modifications, I was able to run your function with your desired outcome (sitewide except on single post/cpt pages & in admin area).

The current preferred method of returning the stylesheet uri is with the get_stylesheet_directory_uri(), not by using get_bloginfo('stylesheet_directory'):

From the WordPress Codex Function Reference page:

'stylesheet_directory' - Returns the stylesheet directory URL of the active theme. (Was a local path in earlier WordPress versions.) Consider using get_stylesheet_directory_uri() instead.

The hook you are using, wp_enqueue_scripts, only loads the items in the front end - not the dashboard or login screen - by default (source: WordPress Codex Plugin API Docs). Try the following modified version of your function, which worked when I tested it locally:

function my_own_scripts() {
    if (! is_single()) {
            wp_register_script ( 'my_javascript_file', get_stylesheet_directory_uri() . '/js/my_javascript_file.js', array( 'jquery' ) );
            wp_enqueue_script ( 'my_javascript_file' );
    }
}
add_action( 'wp_enqueue_scripts', 'my_own_scripts' );
2
  • Thanks for your answer, all makes sense except the "admin" part. I don't want to enqueue the file on admin pages. Anyway, the issue was resolved by changing the function name and the script name. Maybe it was overridden by the theme... Commented Nov 6, 2014 at 15:25
  • Thewp_enqueue_scripts hook only enqueues scripts on the front end; there is a separate hook for enqueueing scripts in the admin, and a third that only enqueues on the login screens. For that reason, there is no need to include !is_admin() in your function. Regarding the naming convention, I would suggest using hyphens as spectators in filenames (aka, my-javascript-file.js). Glad you got it working. Commented Nov 6, 2014 at 18:53
0

To exclude both of these pages/sections you will have to check if the conditions are met for individual section too. Right not you are checking if both conditions are met with AND operator. What you need it OR.

Like this.

function my_own_scripts() {
    if ( !is_admin() || !is_single() ) {
        wp_register_script( 'my_javascript_file', get_bloginfo('stylesheet_directory') . '/js/my_javascript_file.js', array( 'jquery' ) );
        wp_enqueue_script( 'my_javascript_file' );
    }
}
add_action( 'wp_enqueue_scripts', 'my_own_scripts' );
9
  • I think you're wrong. I want to enqueue it if it's not admin page AND not single post. Commented Nov 6, 2014 at 12:15
  • No because both of these will never be true at the same time. That's why it's not working for you. Try it and let me know. Commented Nov 6, 2014 at 12:17
  • I've tried, but doesn't work. Think that I'm checking for NOT true condition (!), so if it is NOT admin page AND it is NOT single post, it will be enqueued. Commented Nov 6, 2014 at 12:19
  • Try is_singular() instead. and see Commented Nov 6, 2014 at 12:32
  • 1
    BTW where are you check this on a page? or inside a post? I just tested it on my website and it's working fine. Are you sure this is not overwritten by any other function in your theme. Commented Nov 6, 2014 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.