WordPress.org

Ready to get started?Download WordPress

Forums

Creating my first plugin, echoing JS and get_option. (7 posts)

  1. markseymour
    Member
    Posted 7 years ago #

    I am trying to create my first plugin, that inserts javascript code into WP_FOOTER. The way that I am trying to make it work is that I have a MySQL table set up to hold a UID, and whenever a blog page is visited, the UID is inserted into the javascript, and then inserted into the page.

    The section of code that I need help with can be found at http://wordpress.pastebin.ca/329566

  2. Austin Matzko
    Member
    Posted 7 years ago #

    This part:

    echo '?>

    doesn't make any sense. If you close the PHP code block with ?>, it automatically echos what follows.

    To make that stuff appear for wp_footer, you'll want to add

    add_action('wp_footer','mes-google-analytics');

    somewhere.

  3. markseymour
    Member
    Posted 7 years ago #

    When I tested the plugin, I got an error within WordPress: Warning: call_user_func_array(): First argumented is expected to be a valid callback, 'mes_google_analytics_options' was given in /home/dreadhol/public_html/hidden/wordpress/wp-includes/plugin.php on line 123

    Would this have to do with function mes_google_analytics($unused)?

    The full code can be found at http://wordpress.pastebin.ca/331028
    note: I am still working on the admin contol panel.

  4. Austin Matzko
    Member
    Posted 7 years ago #

    It looks pretty good except that add_option('mes-google-analytics-uid'); should be called when you're updating an option in the admin panel, so it would be more like

    if ( isset( $_POST['uid'] ) ) {
    update_option('mes-google-analytics-uid',$_POST['uid']);
    }

    and you need quotation marks around the option name in

    get_option('mes-google-analytics-uid')

  5. markseymour
    Member
    Posted 7 years ago #

    I am not entirely sure where I am supposed to place the hooks, I've looked at the code for a couple of plugins, and they are either at the top, the bottom, or interspersed in the code. Right now I have all of my hooks at the bottom:

    //Google Analytics Plugin hooks (bottom)
    //add_option('mes_google_analytics_uid');
    if ( isset( $_POST['uid'] ) ) { update_option('mes-google-analytics-uid',$_POST['uid']); }
    add_action('admin_menu', 'mes_google_analytics_options');
    add_action ('wp_footer', 'mes_google_analytics');

  6. markseymour
    Member
    Posted 7 years ago #

    Okay, I have updated the code at http://wordpress.pastebin.ca/331176 ; The only thing that I can see that needs to be done is the Administration panel, and possibly a link in the Dashboard to Google Analytics login.

    I have the code in for the panel, but it doesn't seem to appear.

    //Adding the Admin options page...
    function mes_google_analytics_options() {
    if (function_exists('add_options_page')) {
    add_options_page('Google Analytics', 'Google Analytics', minuserlevel, basename(__FILE__), 'mes_google_analytics_options_subpanel');
    }
    }

  7. markseymour
    Member
    Posted 7 years ago #

    I hate triple-posting, but I cannot edit my posts past a certain time limit. Anyways, I have the admin page working, it was just a matter of swapping minuserlevel for administrator.

    Now all that I need help with it getting the string entered in the text box into a MySQL table, mes_google_analytics_uid, and a description on how to get the ID from the Google Analytics code. Which BTW I am not sure whether the prefix for all GA ID's are UA-.

    Oh, and BTW, Thanks for helping me, I really appreciate it. :)

Topic Closed

This topic has been closed to new replies.

About this Topic