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)
Viewing 1 replies (of 1 total)
The topic ‘Injecting PHP variable into Javascript’ is closed to new replies.