2016-04-22 20 views
0

Ich schreibe mein erstes WP-Plugin, also fange ich einfach an. Ich dachte, ich würde ein einfaches Plugin erstellen, das ein Google Analytics-Snippet in den Seitenkopf einfügt. Alles scheint zu funktionieren, außer dass die Plugin-Einstellungsseite leer ist. Es sollte ein einfaches Formular geben, wo ich die Analytics UA eingeben kann, aber die Seite ist komplett leer. Ich habe den Code immer wieder durchgesehen und sehe das Problem nicht.Wordpress Plugin Einstellungen Seite

<?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', 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_settings_page(){?> 
    <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', "<?php cc_analytics_settings_group('accountant_name') ?>", 'auto'); 
      ga('send', 'pageview'); 

     </script> 
    <?php } 

Antwort

0

ich das Problem gelöst, indem stattdessen eine PHP-Variable der Verwendung in der Google Analytics-Code aufzurufen, Aufruf der Funktion aus dem Formular auf der Seite Einstellungen.

ga('create', '<?php echo get_option('accountant_name', ''); ?>', 'auto'); 
0

Gerade cc_analytics_menu Argument in add_menu_page umbenennen.

Ihr Code:

add_menu_page('CC Analytics', 'CC Analytics', 'administrator', 'cc_analytics_settings_page', 'cc_analytics_menu', plugins_url('analytics/images/analytics.png')); 

ändern Code:

Umbenannt cc_analytics_menu-cc_analytics_menu_function.

umbenennen auch Callback-Funktion:

add_menu_page('CC Analytics', 'CC Analytics', 'administrator', 'cc_analytics_settings_page', 'cc_analytics_menu_function', plugins_url('analytics/images/analytics.png')); 

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 } 

Voll Plugin-Code:

<?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', "<?php cc_analytics_settings_group('accountant_name') ?>", 'auto'); 
      ga('send', 'pageview'); 

     </script> 
    <?php } 
+0

Das Formular erscheint jetzt im Backend, hurra! Ich habe jedoch festgestellt, dass die Verwendung eines PHP-Aufrufs innerhalb von JavaScript die Seite zum Brechen bringt. Lame. Ich habe diesen Aufruf gemacht, aber ich bin mir nicht sicher, wie ich die Variable erstellen soll: ''". $ TrackerCode. "'' Ich habe versucht, etwas Einfaches wie '$ trackerCode = cc_analytics_menu_function (accountant_name);' aber das hat die Seite kaputt gemacht. –