0

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
  • As an aside the server is in development mode so I suspect the headers sent message is to do with the PHP notices getting sent re the warnings – Narrim Oct 9 at 1:18
2

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.

  • 1
    Sorry I should have been more specific my plugin is called booker...anywhere you see the word plugin it would be booker in the code so the function is activate_booker() in the file booker.php – Narrim Oct 9 at 1:46
  • 1
    Oh one thing I forgot and I am not sure if it matter the file is NAMESPACED namespace Booker; – Narrim Oct 9 at 2:22
  • 1
    So thats confirmed when called a namespaced file even tho its not Classed you would use `register_activation_hook( FILE, 'NameSpace/my_plugin_activate_plugin' ); – Narrim Oct 9 at 2:32
  • 1
    Sally if you update your answer I can accept it thanks for the help – Narrim Oct 9 at 2:33
  • 1
    Great answer covers all the bases – Narrim Oct 9 at 4: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.