The procedure started by simply registering the script with the help of wp_register_script() function. You may take into usage the following code for loading of scripts in WordPress (you should add the code into the functions.php file of your current WordPress theme, e.g.: /public_html/wp-content/themes/theme-name-here/functions.php): It is an easy task to enqueue the scripts in WordPress in a proper manner. It also helps in reducing the load time of page along with avoiding easy conflicts with themes and plug-ins.Įnqueuing scripts in WordPress also ensures that duplicates with the same handle are not loaded twice.Īlways use wp_enqueue_script to load JavaScript files in WordPress. Such a feature allows everybody to utilize the built-in JavaScript libraries that come in a bundled form. With the help of the wp_enqueue_script() function, you can easily inform WordPress the best time to load a script long with dependencies if any. This particular function provides a systematic way of loading JavaScript along with styles. To ensure that everything works properly and that one plugin or theme doesn’t break another, WordPress has an enqueue script function. Thousands of people all around the world design themes and plugins for WordPress. WordPress has a large and strong developer community. Assuming that other plugins will use this jQuery distributed with WordPress, loading a different, custom version will only result in problems. WordPress comes packaged with it’s own version of jQuery which should be used. In case you load jQuery manually along with another plugin that already uses jQuery, it will be loaded twice, resulting in JavaScript errors.Īnother common mistake that plugin and theme developers make is that they load their own, custom version of jQuery which is not recommended. Though easy, it is a wrong way of adding scripts in WordPress. Guys who are not familiar, simply add their scripts through the following code: WordPress has a wp_head() function with a wp_head action hook that enables you to load anything into the head section of the website. It is important to enqueue scripts and styles correctly in WordPress. It is high time to get an overview regarding the best way of enqueuing scripts along with styles in WordPress. This article is useful to any WordPress developer who is creating a WordPress plugin or theme to know what the best practise of enqueuing scripts/styles is. WordPress has an enqueuing system for adding local/remote scripts along with styles to prevent conflicts with plug-ins. Since most users run WordPress with a theme and several plugins, developers are advised to use the correct method of loading scripts into WordPress. * int $hook Hook suffix for the current admin page.Enqueue scripts and styles in WordPress the correct way. * Enqueue a script in the WordPress admin on edit.php. $hook_suffix : (string) The current admin page.Parametersīelow the 1 parameter is required to use this hook. Sometime, you have to remove a registered hook so you can use remove_action to remove admin_enqueue_scripts action. The first parameter admin_enqueue_scripts is name of the hook, The second parameter execute_on_admin_enqueue_scripts_event is name of the function which need to be called, third parameter is the priority of calling the hook if same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function. In the below live example, we have defined a function execute_on_admin_enqueue_scripts_event which takes 1 parameters and we registered using add_action. We at Flipper Code, always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin. To use admin_enqueue_scripts action, first you have to register it using add_action.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |