register_activation_hook() not working

AAAARRRGGGHHHH

So I had an issue with my wordpress plugin not getting initialised properly, my activation hook was not working correctly. If you are in a hurry, my fix was this:

Make sure that the WP_PLUGIN_DIR does NOT have a trailing slash.

Yep, that’s it – two hours of my life tracking down Yet Another 1 Char Bug (I swear I have lost weeks of my life to bugs that are 1 character in nature!).

For more in depth trouble shooting, here are some things to know about the wordpress plugin activation process.

When you call register_activation_hook() it adds an action to the list with your callback associated with it. The action name is something like

activate_test.php

If it working correctly it should appear as above, otherwise it will look something like

activate_var/www/sitename.com/htdocs/wp-content/plugins/test.php

this is bad! The plugin_basename() function that is called has failed to match the WP_PLUGIN_DIR constant and remove it.

Here is the test plugin that I used to help me find the problem

<?php
/**
Plugin Name: test tickles
*/

syslog(LOG_ERR,'Testing @ ' . date('Y-m-d H:i:s') . ' ' . __FILE__);
syslog(LOG_ERR,'WP_PLUGIN_DIR = ' . WP_PLUGIN_DIR);
syslog(LOG_ERR,'plugin base name = ' . plugin_basename(__FILE__));
register_activation_hook(__FILE__, function() {
    syslog(LOG_ERR,"Here is an error");
});

Needless to say, you should keep an eye on the syslog with this code

sudo tail -f /var/log/syslog

And then activate your plugin, if all goes to plan you should see

Here is an error

In your syslog file, if you do not – then you will see the plugin path and the path that plugin_bashname() thinks that it should be. If you see the whole path to your plugin – you now can go and fix it!