options = get_option( 'tracking_option' ); include 'admin/views/html-admin-page.php'; } /** * Register and add settings */ public function page_init() { // Add the section to reading settings so we can add our // fields to it add_settings_section( 'tracking_section', '', array( $this, 'tracking_section_callback_function' ), 'wp-tracking-codes' ); // Add the field with the names and function to use for our new // settings, put it in our new section add_settings_field( 'tracking_analytics', 'Google Analytics UA', array( $this, 'tracking_analytics_callback_function' ), 'wp-tracking-codes', 'tracking_section' ); add_settings_field( 'tracking_analytics_remarketing', 'Google Remarketing Conversion ID', array( $this, 'tracking_analytics_remarketing_callback_function' ), 'wp-tracking-codes', 'tracking_section' ); add_settings_field( 'tracking_facebook_pixel_code', 'Facebook Pixel ID', array( $this, 'tracking_facebook_pixel_code_callback_function' ), 'wp-tracking-codes', 'tracking_section' ); add_settings_field( 'tracking_google_tag_manager', 'Google Tag Manager ID', array( $this, 'tracking_google_tag_manager_callback_function' ), 'wp-tracking-codes', 'tracking_section' ); // Register our setting so that $_POST handling is done for us and // our callback function just has to echo the register_setting( 'wp-tracking-codes', // Option group 'tracking_option', // Option name array( $this, 'sanitize' ) // Sanitize ); } /** * Sanitize each setting field as needed * * @param array $input Contains all settings fields as array keys */ public function sanitize( $input ) { $new_input = array(); if( isset( $input['analytics'] ) ) $new_input['analytics'] = sanitize_text_field( $input['analytics'] ); if( isset( $input['analytics_remarketing'] ) ) $new_input['analytics_remarketing'] = sanitize_text_field( $input['analytics_remarketing'] ); if( isset( $input['facebook_pixel_code'] ) ) $new_input['facebook_pixel_code'] = sanitize_text_field( $input['facebook_pixel_code'] ); if( isset( $input['google_tag_manager'] ) ) $new_input['google_tag_manager'] = sanitize_text_field( $input['google_tag_manager'] ); return $new_input; } /** * Print the Section text */ public function tracking_section_callback_function() { //print 'Enter your settings below:'; } /** * Get the settings option array and print one of its values */ public function tracking_analytics_callback_function() { printf( '
Example: UA-XXXXXXXX-X - Help me
', isset( $this->options['analytics'] ) ? esc_attr( $this->options['analytics']) : '' ); } public function tracking_analytics_remarketing_callback_function() { printf( 'Example: 123456789 - Help me
', isset( $this->options['analytics_remarketing'] ) ? esc_attr( $this->options['analytics_remarketing']) : '' ); } public function tracking_facebook_pixel_code_callback_function() { printf( 'Example: 1234567890 - Help me
', isset( $this->options['facebook_pixel_code'] ) ? esc_attr( $this->options['facebook_pixel_code']) : '' ); } public function tracking_google_tag_manager_callback_function() { printf( 'Example: GTM-XXXXXX - Help me
', isset( $this->options['google_tag_manager'] ) ? esc_attr( $this->options['google_tag_manager']) : '' ); } } if( is_admin() ) $RegisterCodes = new RegisterCodes();