![]() ![]() 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.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. 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.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. 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. This is the unique name of the script so that WordPress can identify it. 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. |