A common action performed by plugins is to add extra content to the header of visitor-facing pages generated by WordPress. This recipe shows you how to register an action hook function to be able to add such additional content. To make this example more concrete, we will use the Google Analytics page header JavaScript code that so many people use to get good page view statistics for their site.
- Navigate to the WordPress plugin directory of your development installation.
- Create a new directory called
ch2-page-header-output
. - Navigate to this directory and create a new text file called
ch2-page-header-output.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 2 - Page Header Output
. - Add the following line of code to register a function that will be called when WordPress renders the page header:
add_action( 'wp_head', 'ch2pho_page_header_output' );
- Add...