ThemeShaper Forums » Thematic

[closed]

Retrieve theme option into Javascript

(5 posts)
  • Started 8 years ago by 11thinking
  • Latest reply from 11thinking
  • This topic is resolved
  1. 11thinking
    Member

    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.

    Posted 8 years ago #
  2. the first part looks correct... can you verify by echoing it out somewhere that you have a value for _googleanalytics in the database? i usually do something like

    function bacon() {
       global $my_shortname;
        $gcode = get_option($my_shortname . '_googleanalytics');
       echo $gcode;
    }
    add_action('thematic_before','bacon');

    but i think your problem is here:

    <script type="text/javascript">
          var _gaq = _gaq || [];
          _gaq.push(['_setAccount', '$gcode']);
          _gaq.push(['_trackPageview']);

    since you are inside a javascript tag (and have closed the php tag previously) but are trying to use a php variable. try

    <script type="text/javascript">
    
    var gcode = <?php echo $gcode; ?> ;
    alert( gcode); //remove when fixed
          var _gaq = _gaq || [];
          _gaq.push(['_setAccount', '$gcode']);
          _gaq.push(['_trackPageview']);

    to debug w/ javascript i usually add an alert, which i did above.

    Posted 8 years ago #
  3. 11thinking
    Member

    That does not work.

    My problem basically is that I have a theme option which can easily be retrieved with
    $blabla = get_option('abc_twitterusr'); (for this example abc is my theme shortcode)

    And I want this blabla to output in a javascript code:

    <script type="text/javascript">
    example: $blabla
    </script>

    It doesnt have to do with Google Analytics, that's just an example. The same way I want with other Javascripts too.

    Posted 8 years ago #
  4. well i messed up my code-

    _gaq.push(['_setAccount', '$gcode']);

    which should have been

    _gaq.push(['_setAccount', gcode]);

    stil dont know if that would work, b/c i don't know about push. but it definitely failed b/c i defined the javascript variable to be simply gcode and then asked for $gcode... which was undefined. brain fart. plus by putting the variable in quotes you stop treating it like a variable and like a string- i think.

    so switching to your easier example...

    <script type="text/javascript">
    example: $blabla
    </script>

    won't work, but

    <script type="text/javascript">
    example: <?php echo $blabla; ?>
    </script>

    should.

    Posted 8 years ago #
  5. 11thinking
    Member

    IT WORKS!! THANKS SO MUCH HELGA!! :D
    You're an angel!

    Posted 8 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.