Debug "register_activation_hook" in WordPress
I'm trying to learn how to write a WordPress plugin, with the goal of writing a user-generated glossary plugin after I asked people on Twitter, which would be helpful (it's good that I can use my learning experience to be useful for more than just I).
In any case, upon installation, the plugin sets up a database table and adds some test data to it. Then, when the content is displayed, the foreach loop changes each phrase and replaces it with a DHTML floating point field.
The problem is, I can't figure out what's going on with register_activation_hook
; it can be called and SQL does not work, or it cannot be called (in any case, I have no additional table in the database after activating the plugin).
The hook looks like this:
register_activation_hook(__FILE__, "bot_install");
And 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 first pardon the ego database entry, this is just for testing ...
Secondly, is there something I should have seen that I missed? And third (and most importantly), how can I debug "bot_install"? Can I just add expressions like:
echo "in xxxx";
or it will mess up the headers (as I think all this code runs before the main output).
a source to share
I don't know anything about WordPress, but ... since there is no error handling in the code, how do you know what $wpdb
is a valid database connection handle and how the code can detect that something is not working - for example, the CREATE TABLE statement fails? The code seemingly blithely assumes that nothing can or will not go wrong, but my experience is that anything can and does go wrong.
I'm still not an expert, but the url it links to is:
Run any query on the database
The query function allows you to execute any SQL query on the WordPress database. Your best bet is to use a more specific function (see below) for SELECT queries.
<?php $wpdb->query('query'); ?>
request
(string) The SQL query you want to execute.
The function returns an integer corresponding to the number of rows touched / selected. If there is a MySQL error, the function will return FALSE. (Note: since both 0 and FALSE can be returned, make sure you are using the correct comparison operator: equality == versus identity ===).
[...]
Examples of
Remove the "gargle" meta key and value from message 13.
$wpdb->query(" DELETE FROM $wpdb->postmeta WHERE post_id = '13' AND meta_key = 'gargle'");
So, suppose if the CREATE TABLE operation fails, you get FALSE from it, which you can test with the appropriate statement - I'm not sure which one is.
Further, due to errors, the page says:
Show and hide SQL errors
You can turn the display of echoes on and off with 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 request using print_error.
<?php $wpdb->print_error(); ?>
Finally, for debugging during initial development, it doesn't really matter if the answer is garbled as long as you can see the information.
a source to share
It's too late to answer the third (and "most important"!) Question.
To debug activation errors, add an action to the hook activated_plugin
to write the output buffer (which will contain your errors) to a file and then examine it.
You can also enable debugging in your config file so you can get more information in 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');
a source to share
I had to deal with this a while ago. There is something tricky with register_activation_hook, for some reason working queries don't 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 this doesn't work.
a source to share