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.
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).
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.
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:
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:
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.
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.
Let me know if that doesn’t work.
Well, there exists something call PHP error log, so, if you want to debug something in WordPress (PHP) you can call
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