<?php
/*
Plugin Name: Custom Code
Description: Add Custom Analytics.
Author:      Rahul
Version:     1.0
License:     GPLv2 or later
*/

/* register menu item */
function custom_code_admin_menu_setup(){
add_submenu_page(
 'options-general.php',
 'Custom Code Settings',
 'Custom Code',
 'manage_options',
 'custom_code',
 'custom_code_admin_page_screen'
 );
}
add_action('admin_menu', 'custom_code_admin_menu_setup'); //menu setup

/* display page content */
function custom_code_admin_page_screen() {
 global $submenu;
// access page settings 
 $page_data = array();
 foreach($submenu['options-general.php'] as $i => $menu_item) {
 if($submenu['options-general.php'][$i][2] == 'msp_helloworld')
 $page_data = $submenu['options-general.php'][$i];
 }

// output 
?>
<div class="wrap">
<?php screen_icon();?>
<form id="custom_code_options" action="options.php" method="post">
<?php
settings_fields('custom_code_options');
do_settings_sections('custom_code'); 
submit_button('Save options', 'primary', 'custom_code_options_submit');
?>
 </form>
</div>
<?php
}
/* settings link in plugin management screen */
function custom_code_settings_link($actions, $file) {
if(false !== strpos($file, 'custom-code'))
 $actions['settings'] = '<a href="options-general.php?page=custom_code">Settings</a>';
return $actions; 
}
add_filter('plugin_action_links', 'custom_code_settings_link', 2, 2);

/* register settings */
function custom_code_settings_init(){

register_setting(
 'custom_code_options',
 'custom_code_options',
 'custom_code_options_validate'
 );

add_settings_section(
 'custom_code_authorbox',
 '', 
 '',
 'custom_code'
 );

add_settings_field(
 'custom_code_authorbox_template',
 'Google Analytics ID', 
 'custom_code_field',
 'custom_code',
 'custom_code_authorbox'
 );
}

add_action('admin_init', 'custom_code_settings_init');

/* validate input */
function custom_code_options_validate($input){
 global $allowedposttags, $allowedrichhtml;
if(isset($input['custom_code_value']))
 $input['custom_code_value'] = wp_kses_post($input['custom_code_value']);
return $input;
}


/* filed output */
function custom_code_field() {
 $options = get_option('custom_code_options');
 $authorbox = (isset($options['custom_code_value'])) ? $options['custom_code_value'] : '';
 $authorbox = esc_textarea($authorbox); //sanitise output
?>
 <input type="text" id="custom_code_value" name="custom_code_options[custom_code_value]"  value="<?php echo $authorbox;?>" />
 
<?php
}

add_action( 'wp_head', 'sweetp_add_analytics' );
function sweetp_add_analytics() {
     $options = get_option('custom_code_options');
 $authorbox = (isset($options['custom_code_value'])) ? $options['custom_code_value'] : '';
 $authorbox = esc_textarea($authorbox); //sanitise output
$analytics = "<script type='text/javascript'>

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', '".$authorbox."']); //Change To Your Account ID
  _gaq.push(['_setDomainName', 'none']);
	_gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>";

echo $analytics;
}