Title: Injecting PHP variable into Javascript
Last modified: August 31, 2016

---

# Injecting PHP variable into Javascript

 *  Resolved [Chris](https://wordpress.org/support/users/ledhdtvreviewsblog/)
 * (@ledhdtvreviewsblog)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/injecting-php-variable-into-javascript/)
 * I’m writing my first plugin, so I wanted to keep it simple. I wrote this to inject
   Google Analytics into the Head tag. I’ve got everything working except I’m not
   sure how to create a PHP variable that I can call into javascript. I feel good
   about the actual javascript call, just not the PHP variabl creation. Here’s the
   code I’ve got so far.
 *     ```
       <?php
           /*
           Plugin Name: Analytics
           Plugin URI: https://chriscather.wordpress.com
           Description: Plugin for adding Google Analytics to website
           Author: C. Cather
           Version: 1.0
           Author URI: https://chriscather.wordpress.com
           */
   
       add_action('admin_menu', 'cc_analytics_menu');
   
       function cc_analytics_menu() {
           add_menu_page('CC Analytics', 'CC Analytics', 'administrator', 'cc_analytics_settings_page', 'cc_analytics_menu_function', plugins_url('analytics/images/analytics.png'));
       }
   
       add_action( 'admin_init', 'cc_analytics_settings' );
   
       function cc_analytics_settings() {
           register_setting( 'cc_analytics_settings_group', 'accountant_name' );
       }
   
       function cc_analytics_menu_function(){?>
           <div class="wrap">
               <h2>Staff Details</h2>
               <form method="post" action="options.php">
                   <?php settings_fields( 'cc_analytics_settings_group' ); ?>
                   <?php do_settings_sections( 'cc_analytics_settings_group' ); ?>
                   <table class="form-table">
                       <tr valign="top">
                       <th scope="row">Accountant Name</th>
                       <td><input type="text" name="accountant_name" value="<?php echo esc_attr( get_option('accountant_name') ); ?>" /></td>
                       </tr>
                   </table>
                   <?php submit_button(); ?>
               </form>
           </div>
       <?php }
   
       add_action( 'wp_head', 'cc_analytics' );
           function cc_analytics() { ?>
               <script>
                   (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                   (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                   m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                   })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
   
                   ga('create', '" . $trackerCode . "', 'auto');
                   ga('send', 'pageview');
   
               </script>
           <?php }
       ```
   
 * I tried creating the variable like this, but it breaks the page.
 * `$trackerCode = cc_analytics_menu_function(accountant_name);`

Viewing 1 replies (of 1 total)

 *  Thread Starter [Chris](https://wordpress.org/support/users/ledhdtvreviewsblog/)
 * (@ledhdtvreviewsblog)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/injecting-php-variable-into-javascript/#post-7318119)
 * I resolved the issue by instead of using a PHP variable to call in the Google
   Analytics code, calling the function from the form in the Settings page.
    `ga('
   create', '<?php echo get_option('accountant_name', ''); ?>', 'auto');`

Viewing 1 replies (of 1 total)

The topic ‘Injecting PHP variable into Javascript’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * 1 reply
 * 1 participant
 * Last reply from: [Chris](https://wordpress.org/support/users/ledhdtvreviewsblog/)
 * Last activity: [10 years, 1 month ago](https://wordpress.org/support/topic/injecting-php-variable-into-javascript/#post-7318119)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
