- Navigate to the WordPress plugin directory of your development installation.
- Create a new directory called ch9-load-jquery.
- Navigate to the directory and create a text file called ch9-load-jquery.php.
- Open the new file in a code editor and add an appropriate header at the top of the plugin file, naming the plugin Chapter 9 - Load jQuery.
- Add the following line of code to register a function to be called when script loading requests are processed:
add_action( 'wp_enqueue_scripts', 'ch9lj_front_facing_pages' );
- Add the following code segment to provide an implementation for the ch9lj_front_facing_pages function:
function ch9lj_front_facing_pages() {
wp_enqueue_script( 'jquery' );
}
- Save and close the plugin file.
- Go to the Themes menu section located under Appearance in the WordPress administration interface.
- Click on Add New and search for a theme called Twenty Eleven.
- Install the theme on your website and Activate it.
- Visit your website and view the page source, searching for instances of the jquery.js library. Your search should come up empty unless you have activated other plugins that are asking for jQuery to be loaded.
- Navigate to the Plugins management page and Activate the Chapter 9 - Load jQuery plugin.
- Go back to any page on your website and view the page source.
- Search for the keyword jquery to see that a copy of the script is now loaded from the WordPress wp-includes folder, along with the jquery-migrate script for backward compatibility:
<script type='text/javascript' src='http://localhost/
wp-includes/js/jquery/jquery.js?ver=1.12.4'></script>
<script type='text/javascript' src='http://localhost/
wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1'></script>