views:

757

answers:

2

Originally posted on the WordPress forums but no-one answered... so I'll try my luck here...

Hi all,

I'm trying to learn to write a WordPress plugin by setting myself a goal of witting 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.

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).

Thanks in advance Scott

+1  A: 

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.

Jonathan Leffler
$wpdb is a global wordpress variable assuming the database exists it poits to it (well a class that handles it as far as I can tell).I agree there's no error handleing, that what I want! the idea behind this project is to learn so I'd rather fix it myself than get someone to tell me whats wrong. But since this function is called before anything is displayed, displaying anything will kill the HTML headers.. So how do I test?
Scott Herbert
no room to cite links in the orginal comment so...re $wpdb see http://codex.wordpress.org/Function_Reference/wpdb_Classand re the debuging, see the last three paragraphs in the questron.Thanks
Scott Herbert
OK thanks for the additional points. I'd missed them.
Scott Herbert
A: 

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.

Andy