- Navigate to the ch2-page-header-output folder of the WordPress plugin directory of your development installation.
- Open the ch2-page-header-output.php file in a text editor.
- Add the following line of code to register a function to be called when WordPress first identifies that the requested page is an administration page:
add_action( 'admin_init', 'ch2pho_admin_init' );
- Add the following code section to provide an implementation for the ch2pho_admin_init function:
function ch2pho_admin_init() {
add_action( 'admin_post_save_ch2pho_options',
'process_ch2pho_options' );
}
- Add the following code section to provide an implementation for the process_ch2pho_options function that was declared in the previous step:
function process_ch2pho_options() {
// Check that user has proper security level
if ( !current_user_can( 'manage_options' ) ) {
wp_die( 'Not allowed' );
}
// Check if nonce field configuration form is present
check_admin_referer( 'ch2pho' );
// Retrieve original plugin options array
$options = ch2pho_get_options();
// Cycle through all text form fields and store their values
// in the options array
foreach ( array( 'ga_account_name' ) as $option_name ) {
if ( isset( $_POST[$option_name] ) ) {
$options[$option_name] =
sanitize_text_field( $_POST[$option_name] );
}
}
// Cycle through all check box form fields and set the options
// array to true or false values based on presence of variables
foreach ( array( 'track_outgoing_links' ) as $option_name ) {
if ( isset( $_POST[$option_name] ) ) {
$options[$option_name] = true;
} else {
$options[$option_name] = false;
}
}
// Store updated options array to database
update_option( 'ch2pho_options', $options );
// Redirect the page to the configuration form
wp_redirect( add_query_arg( 'page',
'ch2pho-my-google-analytics',
admin_url( 'options-general.php' ) ) );
exit;
}
- Save and close the plugin file.
- Click on the Settings section of the administration menu.
- Click on the My Google Analytics menu item to display the configuration page.
- Change the value of one of the fields and click on the Submit button.
- When the page refreshes, you will see that the values displayed reflect the values submitted.