0

what is the best place to put javascript function in wordpress? Is it more appropriate in the function.js or in the function.php file? Is there any difference in term of performance?

2 Answers 2

1

I typically put my custom functions in their own JS files for easy organization then enqueue it in the function.php file.

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

This especially works well if your script has a dependency such as JQuery.

wp_register_script('script-name', get_template_directory_uri() . '/js/myawesomescript.min.js', array('jquery'), '1.0.0');
wp_enqueue_script('script-name'); // Enqueue it!

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Best way depends on if it is dependent on a library or not. how to add scripts in wordpress

You are going to want to add js scripts in a js file though, not the functions.php

we typically use an app.js file

Comments

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.