简体   繁体   中英

best place to put javascript in wordpress function.php or function.js

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM