debugging “register_activation_hook” in WordPress

I’m trying to learn to write a WordPress plugin by setting myself a goal of writing a user generated glossary plugin after I asked people on Twitter what would be useful (well I may as well use my learning experience to be useful for more than just me).

Anyway, on installation the plugin sets up a database table, and adds some test data to it. Then when the content is displayed a foreach loop changes each phrase and replaces it with a DHTML floaty box.

Read More

The problem is, however, I can’t work out what’s going on with the register_activation_hook; it may be being called and the SQL is failing or it may not be being called (either way I don’t have an extra table in the database after I activate the plugin).

The hook looks like this:

register_activation_hook(__FILE__, "bot_install");

And the bot_install code like this

function bot_install()
{
    global $wpdb;
    $table = $wpdb->prefix."sh_gloss";

    $structure = "CREATE TABLE $table (
        id INT(9) NOT NULL AUTO_INCREMENT,
        phrase VARCHAR(80) NOT NULL,
        desc VARCHAR(255) NOT NULL,
    UNIQUE KEY id (id)
    );";
    $wpdb->query($structure);

    // Populate table
    $wpdb->query("INSERT INTO $table(phrase, desc)
        VALUES('Scott Herbert', 'Rockstar Programmer')");

}

OK so firstly please forgive the ego database entry, it’s just for testing…

Secondly is there something I should have seen that I’ve missed?
And thirdly (and most importantly) how can I debug “bot_install”? Can I just add statements like:

echo "in xxxx";

or will that mess up the headers (since I guess all this code is ran before the main output).

Related posts

Leave a Reply

4 comments

  1. To answer your third (and “most important”!) question, very late.

    To debug activation errors, add an action on the activated_plugin hook, to write the output buffer (which will contain your errors) to a file, and then examine that.

    You may also want to turn on debugging in the config file, so you get more information going to your file.

    function save_output_buffer_to_file()
    {
        file_put_contents(
          ABSPATH. 'wp-content/plugins/activation_output_buffer.html'
        , ob_get_contents()
        );
    }
    add_action('activated_plugin','save_output_buffer_to_file');
    
  2. I know nothing about WordPress, but…since there is no error handling in the code, how do you know that $wpdb is a valid database connection handle, and how could the code detect that something is not working – for example, the CREATE TABLE statement failed? The code apparently blindly (blithely) assumes nothing can or will go wrong, but my experience is that things can and do go wrong.


    I’m still not the expert, but the URL referenced says:

    Run Any Query on the Database

    The query function allows you to execute any SQL query on the WordPress database. It is best to use a more specific function (see below), however, for SELECT queries.

    <?php $wpdb->query('query'); ?> 
    

    query

    (string) The SQL query you wish to execute.

    The function returns an integer corresponding to the number of rows affected/selected. If there is a MySQL error, the function will return FALSE. (Note: since both 0 and FALSE can be returned, make sure you use the correct comparison operator: equality == vs. identicality ===).

    […]

    Examples

    Delete the ‘gargle’ meta key and value from Post 13.

       $wpdb->query("
           DELETE FROM $wpdb->postmeta WHERE post_id = '13'
                 AND meta_key = 'gargle'");
    

    So, presumably, if the CREATE TABLE operation fails, you will get a FALSE back from it, which you can test with the appropriate operator – I’m not sure which one it is.

    Further down, on the subject of errors, the page says:

    Show and Hide SQL Errors

    You can turn error echoing on and off with the show_errors and hide_errors, respectively.

    <?php $wpdb->show_errors(); ?>
    <?php $wpdb->hide_errors(); ?> 
    

    You can also print the error (if any) generated by the most recent query with print_error.

    <?php $wpdb->print_error(); ?> 
    

    Finally, for debugging while under initial development, it won’t matter too much if the response is garbled as long as you can see the information.

  3. I had to deal with this a while back. There is something tricky with register_activation_hook, for some reason running queries did not work for me, use the dbDelta function.

    function bot_install()
    {
        global $wpdb;
        $table = $wpdb->prefix."sh_gloss";
    
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    
        $structure = "CREATE TABLE $table (
            id INT(9) NOT NULL AUTO_INCREMENT,
            phrase VARCHAR(80) NOT NULL,
            desc VARCHAR(255) NOT NULL,
            UNIQUE KEY id (id));";
    
        dbDelta($structure);
    
    
        // Populate table
        $wpdb->query("INSERT INTO $table(phrase, desc)
            VALUES('Scott Herbert', 'Rockstar Programmer')");
    
    }
    

    Let me know if that doesn’t work.

  4. Well, there exists something call PHP error log, so, if you want to debug something in WordPress (PHP) you can call

    error_log(ob_get_contents(), 3, plugin_dir_path(__FILE__) . 'errors');
    

    in this little line, you will create a file called error, in the plugin directory.

    PHP error_log Manual

    EDIT: Sometimes this approach is a better solution because PHP always has permissions to write down the file, which is the opposite in file_put_contents