• Hi all. I’m trying to include and run a simple Javascript file on my site, but am encountering an error. My debug.log file reads:

    PHP Notice:  wp_enqueue_script was called <strong>incorrectly</strong>. Scripts and styles should not be registered or enqueued until the <code>wp_enqueue_scripts</code>, <code>admin_enqueue_scripts</code>, or <code>login_enqueue_scripts</code> hooks. Please see <a href="http://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information. (This message was added in version 3.3.) in /home/artslamm/public_html/wp-includes/functions.php on line 3547

    I’m not sure what this means. Can anyone shed some light?

    Here are the steps I carried out.

    The script consists of one line only:

    alert('Hello, World!')

    and is in a file ‘test.js,’ which resides in the same directory as my functions.php file.

    To add this script to WordPress, I inserted the following code into my functions.php file:

    function add_test_script() {
        wp_enqueue_script(
            'test', // name your script so that you can attach other scripts and de-register, etc.
          get_template_directory_uri() . 'test.js', // this is the location of your script file
            array('jquery') // this array lists the scripts upon which your script depends
        );
    }
    add_action( 'wp_enqueue_scripts', 'add_test_script' );

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Are you also calling the function add_test_script() somewhere else in your theme template files?

    Thread Starter cag8f

    (@cag8f)

    Thanks for the reply. I hadn’t called the function anywhere. I was under the impression the function add_test_script() would be automatically called in the header. Regardless, I added the line <?php testFunction();?> to my header.php. But alas the script is still not running and I am still receiving the same log file error. Any thoughts?

    edit: I managed to get the script running properly. I am using a child theme, so I changed get_stylesheet_directory_uri() to get_template_directory_uri() and the function is now called in the header. But the error message still remains in the log file. I am not aware of any other calls to this function. I have checked my child functions.php, the parent functions.php, and the child header.php (all files where I may have made this function call), but no such function call exists. Any thoughts?

    The log file indicates that there is an error with a certain line of PHP code. But does it indicate in what file and what line number that PHP code exists? If not, is there WordPress functionality to obtain that information (either built-in, or via a plugin)?

    Had a similar PHP notice:

    [30-Jul-2015 03:38:42 UTC] PHP Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /home/content/68/number/html/wp-includes/functions.php on line 3560

    It does not refer to a theme or plugin file, just a core file. Hard to debug this way with loads of plugins and theme files. But I guess you and I could start with going through the theme files.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Beginner JQUERY help: "wp_enqueue_script was called incorrectly"’ is closed to new replies.