Wordpress Plugin Activate / Deactive Failing

Below is my plugin.php file that would install into wp-content

 ?php    // If this file is called directly, abort.
    namespace Booker;
    if ( ! defined( 'WPINC' ) ) {
        die;
    }

 /**
 * Currently plugin version.
 * Start at version 1.0.0 and use SemVer - https://semver.org
 * Rename this for your plugin and update it as you release new versions.
 */
define( 'PLUGIN_NAME_VERSION', '1.0.0' );

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-booker-activator.php
 */
function activate_booker() {
    require_once plugin_dir_path(__FILE__) . 'includes/class-booker-activator.php';
    error_log("Running Activation");
    Booker_Activator::activate();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-booker-deactivator.php
 */
function deactivate_booker() {
    require_once plugin_dir_path(__FILE__) . 'includes/class-booker-deactivator.php';
    error_log("Running Deactivation");
    Booker_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'activate_booker');
register_deactivation_hook( __FILE__, 'deactivate_booker');

?

It certainly is not executing but the plugin deactivates and activates as expected. Currently, there is no code in the function calls defined inside those functions....but the lack of the log message shows no execution. However, I am getting the problem:

The plugin generated 2368 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

And in my logs I get:

[08-Oct-2019 23:51:20 UTC] PHP Warning:  call_user_func_array() expects parameter 1 to be a valid callback, function 'activate_booker' not found or invalid function name in G:\wamp64\www\wordpress\wp-includes\class-wp-hook.php on line 286
[08-Oct-2019 23:51:20 UTC] PHP Stack trace:
[08-Oct-2019 23:51:20 UTC] PHP   1. {main}() G:\wamp64\www\wordpress\wp-admin\plugins.php:0
[08-Oct-2019 23:51:20 UTC] PHP   2. activate_plugin() G:\wamp64\www\wordpress\wp-admin\plugins.php:44
[08-Oct-2019 23:51:20 UTC] PHP   3. do_action() G:\wamp64\www\wordpress\wp-admin\includes\plugin.php:672
[08-Oct-2019 23:51:20 UTC] PHP   4. WP_Hook-do_action() G:\wamp64\www\wordpress\wp-includes\plugin.php:465
[08-Oct-2019 23:51:20 UTC] PHP   5. WP_Hook-apply_filters() G:\wamp64\www\wordpress\wp-includes\class-wp-hook.php:310

And also

[09-Oct-2019 00:44:43 UTC] PHP Warning:  call_user_func_array() expects parameter 1 to be a valid callback, function 'deactivate_booker' not found or invalid function name in G:\wamp64\www\wordpress\wp-includes\class-wp-hook.php on line 286
[09-Oct-2019 00:44:43 UTC] PHP Stack trace:
[09-Oct-2019 00:44:43 UTC] PHP   1. {main}() G:\wamp64\www\wordpress\wp-admin\plugins.php:0
[09-Oct-2019 00:44:43 UTC] PHP   2. deactivate_plugins($plugins = *uninitialized*, $silent = *uninitialized*, $network_wide = *uninitialized*) G:\wamp64\www\wordpress\wp-admin\plugins.php:192
[09-Oct-2019 00:44:43 UTC] PHP   3. do_action($tag = *uninitialized*, $arg = *uninitialized*) G:\wamp64\www\wordpress\wp-admin\includes\plugin.php:792
[09-Oct-2019 00:44:43 UTC] PHP   4. WP_Hook-do_action($args = *uninitialized*) G:\wamp64\www\wordpress\wp-includes\plugin.php:465
[09-Oct-2019 00:44:43 UTC] PHP   5. WP_Hook-apply_filters($value = *uninitialized*, $args = *uninitialized*) G:\wamp64\www\wordpress\wp-includes\class-wp-hook.php:310

Additional error when I changed the function names:

[09-Oct-2019 02:08:03 UTC] PHP Warning:  call_user_func_array() expects parameter 1 to be a valid callback, function 'booker_deactivate' not found or invalid function name in G:\wamp64\www\wordpress\wp-includes\class-wp-hook.php on line 286
[09-Oct-2019 02:08:03 UTC] PHP Stack trace:
[09-Oct-2019 02:08:03 UTC] PHP   1. {main}() G:\wamp64\www\wordpress\wp-admin\plugins.php:0
[09-Oct-2019 02:08:03 UTC] PHP   2. deactivate_plugins() G:\wamp64\www\wordpress\wp-admin\plugins.php:192
[09-Oct-2019 02:08:03 UTC] PHP   3. do_action() G:\wamp64\www\wordpress\wp-admin\includes\plugin.php:792
[09-Oct-2019 02:08:03 UTC] PHP   4. WP_Hook-do_action() G:\wamp64\www\wordpress\wp-includes\plugin.php:465
[09-Oct-2019 02:08:03 UTC] PHP   5. WP_Hook-apply_filters() G:\wamp64\www\wordpress\wp-includes\class-wp-hook.php:310

Topic deactivation activation plugin-development Wordpress

Category Web


So the 'activate_plugin' was a typo in the question. But even so, as a friendly reminder (for all of us), activate_plugin() is an existing function in WordPress. And you should use unique prefix in your custom functions (that are defined in the global scope):

function my_plugin_activate_plugin() {
  ...
}

register_activation_hook( __FILE__, 'my_plugin_activate_plugin' );

The Problem and Its Solution

The error/warning in the logs which says function 'activate_booker' not found or invalid function name happens because you're using a namespace in your file and you failed to properly reference to the callback function in your namespace:

// Incorrect - These are referencing to global functions.
register_activation_hook( __FILE__, 'activate_booker' );
register_deactivation_hook( __FILE__, 'deactivate_booker' );

// Correct syntax when referencing to namespace functions.
register_activation_hook( __FILE__, 'Booker\activate_booker' );
register_deactivation_hook( __FILE__, 'Booker\deactivate_booker' );

// Or use the __NAMESPACE__ constant.
register_activation_hook( __FILE__, __NAMESPACE__ . '\activate_booker' );
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\deactivate_booker' );

Note though, unless a fatal error was encountered, the plugin would still be activated/deactivated; it's just that the activation/deactivation callback wouldn't be called.

And here's a good article which talks about using PHP namespaces in WordPress.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.