So before we enqueue our own version of jQuery, we must de-register the WP version. We could simply enqueue it, but then there would be two copies/versions of jQuery loaded on the page (yours and WP’s). Now, let’s say that we want to use a version of jQuery that is different than the one that is included with WordPress. So when we declare our custom script, we set the $deps parameter equal to array('jquery') and call it a day. Wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ) For reference, here are the parameters used for wp_enqueue_script(): This declares jQuery as a dependency for my-custom-script, so WordPress automatically will load its own copy of jQuery. '/js/my-custom-script.js', array('jquery'), null, true) The smart loading requires that you specify jquery as the $deps parameter when enqueuing your other JavaScript files, for example: wp_enqueue_script('my-custom-script', get_template_directory_uri(). These days, WordPress automatically loads jQuery for you, whenever some other script requires it. Much has changed with WP script loading since this article was posted. How was your plugin to know you already had it loaded? For one thing, some plugins use the jQuery library, and they are going to load it as well. It also reduces the likelihood that our plugins scripts and styles will conflict with another plugin or theme.If you want, you can just download jQuery, put it on your server and link to it from your header.php file in the section. The reason why is we don’t want to slow down the site our plugin is installed on with unnecessary scripts or styles. It’s a good practice to only load the styles and scripts in your plugin when they are needed. We include the version for the same reason as before so that the user’s cache can be cleared if we update the stylesheet. We have a unique handle, the URL to the stylesheet, dependencies (in this case we have none), and the version of the stylesheet. Settings for this function are similar to wp_register_script. Stylesheets can easily be added using wp_enqueue_style. We use this so that other plugins or the site owner can remove our script if they want to without having to touch our plugin’s code. The wp_enqueue_script() function is what allows WordPress to process your script and output it where it is needed. In Footer: This indicates whether or not our script is loaded in the footer. To achieve this you will need to make use of an anonymous function call hooked on wpfooter action, Copy. If you leave this off, a user’s browser could have the old version cached, and may not see the changes until they clear their cache. NOTE: with new block themes such as Twenty Twenty Two, wplocalizescript / wpaddinlinescript will no longer work if called too late such as in a shortcode rendering callback function. If you ever update the plugin and change the contents of your script.js file, you can increment the version number (1.1, 2.0, or whatever) to force browsers to fetch a new version of the file. The version number helps with caching the script file. Version: We can optionally provide a version number.This indicates to WordPress to load the jQuery library that is included with WordPress. In this case, ours is dependent on jQuery. Dependencies: This specifies other JavaScript libraries that our script is dependent upon.This topic was modified 1 year, 7 months ago by strarsis. This occurs with latest WordPress 5.9 but also with the previous WordPress 5.8.3 release. And an additional backend error: The block reblex-widget has issues. 1 Answer Sorted by: 2 To use wpenqueuescript () correctly, you need to launch your function with the action wpenqueuescripts. The plugins_url function makes it easy for us to add this, without needing to know the exact URL of the file, since our plugin could be installed on many other sites. PHP warning in the widget area: wpenqueuescript was called incorrectly. In this case, we’re using a file called script.js, which is located in the same folder as the file that is adding this function. Source: This is the location of the file we want to load.This helps makes sure the script is only loaded once. Support Fixing WordPress wp-admin page forbidden 403 wp-admin page. This is the unique name of the script so that WordPress can identify it. Using wp-scripts ver 5.0.0+ build step will also produce an file. Handle: This is the ‘sd_my_cool_script’ portion.There are five pieces to the wp_register_script function. Wp_enqueue_style( 'sd_my_styles', plugins_url( 'styles.css', _FILE_ ), '', '1.0' ) Īdd_action( 'wp_enqueue_scripts', 'sd_add_scripts' ) Wp_enqueue_script( 'sd_my_cool_script' ) Wp_register_script( 'sd_my_cool_script', plugins_url( 'script.js', _FILE_ ), array( 'jquery' ), '1.0', true ) Function sd_load_script ( ) Īdd_action ( 'wp_enqueue_scripts', 'sd_add_scripts' )
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |