• 0
Sign in to follow this  
Followers 0

How to add a script (loaded from child-theme folder) and inline javascript to the footer properly?


Question

Posted (edited) · Report post

Hello!

What is the best way to achieve this? 

If I wan to add a  anything.js script file to the footer, and add some inline javascript (to for example load the js plugin I just inserted), how should I do this?

Should I just add them to the footer.php?

What if I want a custom-made section to insert them in the footer?  Most of the time they need to be loaded after jQuery, and often it's also best if they're after the site content. 

 

I can load a script with:

function section_styles(){
// This will load the 'cooljavascript.js' file from the current section folderwp_enqueue_script( 'my-js-plugin', $this->base_url.'/cooljavascript.js', array( 'jquery' ), PL_CORE_VERSION, true );}

But how to initalize the plugin I just loaded?

 

I also want them to be in my project under git and/or in the specific section that needs the script and initialize it, I don't want them i.e with Hooker so it's in the database. 

So can I use some hook in my functions.php? 
Also, can I use hooks to insert js or other things to the site in custom made functions?

Thanks!

Edited by ketri

Share this post


Link to post
Share on other sites

3 answers to this question

  • 0

Posted · Report post

What if my scripts are linked with a section?

For example I'd like to load masonry.js with a section that gets posts and uses masonry on them? 
Can I use hooks in sections?

 

 

Hi,

 

You can use hooks in your functions.php file, that is the primary reason for a functions.php file. You should use the Wordpress enqueue.

 

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Share this post


Link to post
Share on other sites
  • 0

Posted · Report post

Hi there,

 

If you activate the actionmap plugin which comes within the Pro Tools plugin this will show you all the hooks on the page, inside of each main area/section etc. Usually each section will have 4 hooks inside it. The action map plugin will show you where these hooks are positioned on each page, then you can use the one closest to the area you require to position your script to activate.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0