• Hi there,

    I currently have a JS file enqueued which appears towards the bottom of the page, but I need it to be added right at the bottom of the page.

    How can I do this?

    This is the current code I have:

    wp_enqueue_script( 'custom', get_stylesheet_directory_uri() . '/js/custom.js', array( 'jquery' ), false, true );

    Many thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The code you provided is using the WordPress function wp_enqueue_script() to load a JavaScript file named custom.js.

    The fifth argument, true, tells WordPress to load the script in the footer, which means that the script will be included just before the closing </body> tag.

    So, if you want to ensure that this script is loaded exactly at the bottom of the page, you don’t need to make any changes to this code. The script will be loaded in the footer, which is where it should be for best performance.

    Thread Starter thetoolman123

    (@thetoolman123)

    I see, but I have an element that is generated after that file is fired. Is there a way to get it right before the closing body tag, or could I place it there manually?

    can you post a link to the page you’re referring to and describe the element so I can see what the problem is?

    • This reply was modified 1 year, 9 months ago by janet4now.
    • This reply was modified 1 year, 9 months ago by janet4now.
    Thread Starter thetoolman123

    (@thetoolman123)

    Sure:
    https://www.bansteadlocal.co.uk/

    If you click the search icon on desktop, it opens a full screen search. When the options in the dropdown on the left of the search are selected, it should change the content below (popular searches)

    f you want to load your custom JavaScript file after plugin scripts, you can use the wp_enqueue_script function with a higher priority in the hook used for enqueuing scripts.

    In WordPress, scripts are typically enqueued using the wp_enqueue_scripts action hook. To load your custom script after plugin scripts, you can use a later priority in the add_action call for the hook:

    add_action( ‘wp_enqueue_scripts’, ‘enqueue_custom_script’, 100 );

    function enqueue_custom_script() {
    wp_enqueue_script( ‘custom’, get_stylesheet_directory_uri() . ‘/js/custom.js’, array( ‘jquery’ ), false, true );
    }

    In this example, the add_action call is using a priority of 100, which is a higher priority than the default priority of 10. This means that your custom script will be enqueued after any scripts that were enqueued with the default priority of 10.

    Keep in mind that the exact priority you need to use may depend on the plugins you have installed, as well as the order in which they enqueue their scripts. You may need to experiment with different priorities to find the one that works best for your use case.

    Thread Starter thetoolman123

    (@thetoolman123)

    Thank you!

    That makes sense and it worked perfectly. Thank you for explaining that as well, very helpful!

    Just a side note, when copying the code, the quotes are displayed as ’ rather then the ‘ (the slight variation of the quote mark) so I had to change those, but that’s just a copy and paste issue.

    Thanks again!

    Yeah, glad it worked for you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Adding JS right at the bottom of the page?’ is closed to new replies.