![]() function mythemename_all_scriptsandstyles() add_action( 'wp_enqueue_scripts', 'mythemename_all_scriptsandstyles' ) Let’s create a function in functions.php that will store all our javascript and CSS files to use and with that function we need to instruct WordPress to load the function by adding an action. If you are using a child theme which you should be and can’t see a functions.php file then just create one and ensure the file starts with an opening PHP tag. ![]() So where do you add in the scripts and CSS? well, that’s all done in your functions.php file of your theme located in the theme folder and added in as a unique function. The difference between register and enqueue is subtle and in fact, you could just get by with just using the enqueue step by itself but for sake of doing it the proper way lets use both processes. ![]() The register process wp_register_script() for Javascript or wp_register_style() for CSS, basically tells WordPress about the script, and the enqueue process wp_enqueue_script () for Javascript or wp_enqueue_style() for CSS, tells WordPress to run/load the script. The correct method in WordPress to load javascripts and CSS files is to register the scripts and then to enqueue the script. This really is a no no! as this can result in script conflicts or duplications or just downright badness, there is a logical method to add all javascript and CSS files and for them to be recognised properly by WordPress and loaded safely in either the header or footer of the page and to have any other dependent scripts also loaded without conflict. As a hack every now and then I used to add in javascript or CSS files with links hardcoded directly into the header.php or footer.php template files of a WordPress theme. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |