Support » Developing with WordPress » Calling a Function from an Enqueued Script

  • Resolved joshuaofboston

    (@joshuaofboston)


    Beginner question, I think:

    I have been trying to create an external javascript file and call a function from that file in my index.php file in my child theme. The file name is dadsTest.js (my father, a graphic designer, has been equally stumped with this problem); the function is dadstest().

    I’ve gotten so far as to be able to successfully register and enqueue the file, and test that using the wp_script_is function to be sure that I’ve done so. See the code below for that, as it appears in my child theme’s functions.php:

    function wpb_adding_scripts() {
    wp_register_script('dadstest', get_stylesheet_directory_uri('public_html/practice/wp-admin/js/dadstest.js', __FILE__), array('jquery'),'1.1', true);
    wp_enqueue_script('dadstest');
    }
    
    add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );

    dadstest.js, by the way, is written as this:

    function dadstest() {
    	document.getElementById("demo").innerHTML = "Hello World!";
    }
    dadstest()

    Right now I’m just trying to find out if I can actually call a function from an external script, so all this function does is fill a <p> element with text.

    I’ve tried loading the js file in my child theme’s index.php, but no avail; the conditional I’ve written using wp_script_is works, but my call to the js file doesn’t. My code inside that index.php appears below:

    <p id="demo"></p>
    			<?php
    			  $handle = 'dadstest';
       			  $list = 'enqueued';
         	 	  if (wp_script_is( $handle, $list )) { ?>
    				<script>
    			  		document.getElementById("demo").innerHTML = "Test Passed!";
    				</script>
    			<?php
    			  } else { ?>
    				<script>
    				 	document.getElementById("demo").innerHTML = "Test Failed!"
    				</script>
    			<?php
    			  } ?>
    			<script type="text/javascript" src="public_html/practice/wp-admin/js/dadstest.js"></script>

    I’ve tried this by not calling dadstest() in the js file and instead calling it in a <script> element that appears at the end of what I’ve written in index.php:

    <script type="text/javascript">dadstest()</script>

    I was following this post here: https://wordpress.org/support/topic/how-to-call-script-after-enqueuing/

    But I don’t seem to be having the luck that this guy had. Can anyone tell me where I might be making a mistake?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The first thing that stands out is that you are most likely calling the function before the element that it references has been loaded by the browsers DOM.

    This is the “standard” way to execute scripts, especially when they need to be sure that the page has loaded everything that’s needed:

    function dadstest() {
        document.getElementById("demo").innerHTML = "Hello World!";
        console.log ("DEBUGGING - This message sill show in your console window when the function is called.");
    }
    
    jQuery (document).ready (function () {
        dadstest();
    });

    I’ve saved those two functions above over my original .js file and uploaded it. Still no luck.

    I’m still new to this; I didn’t know how to check the console before. But when loading my webpage and looking at it now, I get the error:

    GET http://joshuakery.com/practice/public_html/practice/wp-admin/js/dadsTest.js

    It gives the line number of my html script element as the source of the error.

    So I guess that this script element is still failing to find my .js file?

    wp_script_is() will only tell you if the script is being enqueued, but won’t know if the URL is wrong.

    What the GET error is telling you is that http://joshuakery.com/practice/public_html/practice/wp-admin/js/dadsTest.js doesn’t exist. The fact you’re receiving the error means that the script is enqueuing correctly (somewhere on the page is a script tag with this URL), but the console error is telling you that the URL doesn’t go anywhere.

    The problem is a slight misuse of get_stylesheet_directory_uri() in this part:

    wp_register_script('dadstest', get_stylesheet_directory_uri('public_html/practice/wp-admin/js/dadstest.js', __FILE__), array('jquery'),'1.1', true);
    

    Assuming your code is the the /js folder of your theme, it should look like this:

    wp_register_script('dadstest', get_stylesheet_directory_uri() . '/js/dadstest.js', array('jquery'), '1.1', true);
    

    I can’t say if that will work though, because you haven’t said where the Javascript file is. You will need different functions for locating it if it’s in a plugin or parent theme etc. It definitely shouldn’t be amongst core files in wp-admin.

    • This reply was modified 1 year, 11 months ago by  Jacob Peattie.

    It works!

    Thanks. You nailed the problem.

    I did have my .js file among my core files, but I created a js folder in my child theme and moved my .js file there. And then correcting get_stylesheet_directory_uri did the trick.

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Calling a Function from an Enqueued Script’ is closed to new replies.