![]() ![]() Just put below code to theme’s functions.php file. Using the Google CDN-hosted version of jQuery is another performance gain, because it will be cached for any visitors who have loaded another site that uses the same version. In below code snippets we have unregistered the default jQuery library from default WordPress, then added the Google CDN version after closing the footer tag. Here’s what the path should look like: child-theme-directory/js/follow.js Additional Speed Optimization Tips: This file includes the jQuery that will control the position of the sidebar follow section. In your Child Theme’s folder, create another folder called js, and then create a new file called follow.js and place that inside. '/js/follow.js', array( 'jquery' ), '', true ) Wp_enqueue_script( 'follow', get_stylesheet_directory_uri(). Enqueue sticky menu script - Crunchify TipsĪdd_action( 'wp_enqueue_scripts', 'crunchify_enqueue_script' ) Add below code to your child theme’s functions.php file. Here is a quick hook I’m using in my Genesis Framework theme for right sidebar sticky follow section. It allows for greater control of when and where JS loads on your site. This also has the benefit that every time you save any file while still developing it, you don’t have to take care of the cache, your browser will always load the latest file.Using wp_enqueue_script is the best way to load JavaScript and jQuery into your WordPress template. Nowadays I usually use the latest apporach. Simply put, instead of having all your code in one place, which would slow down your load time, you use the enqueue function. But then you have to update all these string (for all files) manually and it’s easy to forget one, which will result in the browser loading old files from it’s cache. using the wpenqueuescript and wpenqueuestyle functions, you inform WordPress when and where to load custom scripts and stylesheets. The main aspect of enqueueing is that it can improve website performance by reducing page load times and help avoid script conflicts when different plugins or WordPress. In the past, I have used the first or third option. The wpenqueuescript () and wpenqueuestyle () functions instruct the WordPress server to add these scripts and stylesheets to a queue to load on your website’s front end. A dynamic modification date of the file that’s enqueued.A static modification date of the file that’s enqueued.A static modification date of the plugin.A static version number matching the version of the plugin.As your files will probably not (only) change when WordPress gets updated, you should use a different version string. This version number is meant to help you with caching. If you don’t define one yourself, it would append the current version number of WordPress, which today would be 5.9, to the end of the URL. For child themes: get_stylesheet_directory_uri()Īs you can see in the example above, WordPress will add a version number.For themes: get_template_directory_uri().For plugins: plugins_url() or plugin_dir_url().These are the ones, you would usually use: '/js/custom-script.js' ) wpenqueuescript ( 'custom-script' ) Should simply be: wpenqueuescript ( 'custom-script', gettemplatedirectoryuri(). Enqueue the script or style: Once youve registered a script or style, you can enqueue it using the wpenqueuescript or wpenqueuestyle function. If you enqueue a file in a plugin or theme, there are different functions you can use. This: wpregisterscript ( 'custom-script', gettemplatedirectoryuri(). This might seem OK for most of you, but as the wp-content folder can have a different name (some security plugins do that – which is usually not a great idea), you should always use helper functions to get the relative path to that folder. In the example above, we use a relative path to the wp-content/plugins folder. While this would work, there are some issues with this simple way. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |