0

I have been working on my first plugin (call is myplugin). The path to the plugin code is wp-content/plugins/myplugin/myplugin.php. The myplugin.php file is essentially as follows:

<?php
defined( 'ABSPATH' ) OR exit;
/*
 * Plugin Name:  My Plugin
 */

function my_plugin_activation() {
    global $wpdb;
    // SQL code to insert custom database tables
    // using CREATE TABLE IF NOT EXISTS tablename....
}

function my_plugin_deactivation() {   
    // SQL code to update table on deactivation
}

function my_plugin_uninstall() {
    global $wpdb;
    // SQL code to remove custom database tables
}

register_activation_hook( __FILE__, 'my_plugin_activation');
register_deactivaction_hook( __FILE__, 'my_plugin_deactivation');
register_uninstall_hook( __FILE__, 'my_plugin_uninstall');
?>

When I activate the above plugin code the tables are created successfully by the my_plugin_activation routine, the plugin is activated (I get a Deactivate link when if finishes activating), yet the following messages are displayed at the top of the Plugins screen:

Plugin could not be activated because it triggered a fatal error.

Fatal error: Cannot redeclare my_plugin_activaction() (previously declared in C:\xampp\htdocs\deve1wp\wp-content\plugins\myplugin\myplugin.php: 15) in C:\xampp\htdocs\deve1wp\wp-content\plugins\myplugin\myplugin.php on line 198

Line 15 is the actual function definition. Line 198 is the line where I register the activation hook.

What gives?

Thanks in advance.

  • Does it happen if the functions don't do anything, i.e. they are as you posted them in this question? – Andrew Bartel Nov 18 '14 at 16:51
  • 1
    plugin activation is a weird thing, WordPress has to do it in such a way as to not take everything down if a plugin activation triggers a fatal error, and as such, I've read reports that any error manifests itself as a "cannot redeclare" error despite that not actually being the case. make sure your activation and deactivation code outside of the activation/deactivation context has no errors. also have a look at the active_plugins option to make sure your plugin isn't already in that list when it's actually deactivated. – Milo Nov 18 '14 at 17:06
  • You code above works fine (minus typo). I just tried it. IMO, you have something else that is interferring. Probably some error within the code that you are not showing. – user42826 Nov 18 '14 at 17:18
  • Yea that's what I was thinking @Milo – Andrew Bartel Nov 18 '14 at 17:53
1

I found the problem. I didn't realize this was a generic message for pretty much all errors. I stripped everything out of the activation routine (similar to above) and ran it... got no errors. Started adding "stuff" back. And it appears a call to flush() at the end of the activation function trying to force the /wp-content/debug.log to be flushed to disk was triggering the error.

Thanks to everyone here that helped! ;)

0

You have a typo on this line:

register_deactivaction_hook( __FILE__, 'my_plugin_deactivation');

It should be:

register_deactivation_hook( __FILE__, 'my_plugin_deactivation');
  • Thanks. The typo is only in the sample above. I spelled everything correctly in my actual plugin code. So that is not the root issue. The error message makes me think that it sees the register_*_hook as trying to redeclare the functions being attached to those hooks. I have found tons of instances of people complaining about this online, but no real solutions posted. – Jim Nov 18 '14 at 16:47
  • Are you trying this with a fresh install of WP, and with what version of PHP? The above works for me (less the typo of course) with WP 4.0, PHP 5.6.2. You might also see if XAMPP might be throwing things off. – pwbred Nov 18 '14 at 16:52
  • Thanks. I'm running a new install of WordPress 4.0, PHP 5.5.15, and Twenty Fourteen theme. – Jim Nov 18 '14 at 17:02
  • Have you tried explicitly calling the plugin path? As in something like: WP_PLUGIN_DIR . '/myplugin/myplugin.php' – pwbred Nov 18 '14 at 17:05
  • 1
    Glad you figured it out! Bet you're already going to do this, but probably a good to idea to explain that in answer here for posterity. – pwbred Nov 18 '14 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.