I'm trying to add several theme options that needs javascript, one of them is the following:
It's a google analytics insert code. In the options page, the user can put UA-XXXXXXXX-X into the text box, I want to retrieve that filled in code into the google analytics script:
function analytic_header() {
global $my_shortname;
$gcode = get_option($my_shortname . '_googleanalytics'); ?>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '$gcode']);
_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><?php
}
add_action ('wp_head', 'analytic_header');
I use $gcode = get_option($my_shortname . '_googleanalytics'); to retrieve what the user has filled in, but it doesn't work.
I don't want the user to fill in the whole Google Analytics code, as that seems inconvenient, only UA-XXXXXXXX-X is important, the rest is exactly the same for every Google Analytics account.