0

I'm developing a plugin that needs a certain php extension (sqlite3) to be available, or it can't work correctly.

Is there a way I can present an error message to the site owner when they try to activate it, saying something like this?

Sorry, you can't use this. Because yadda yadda. Click here for more information.

It doesn't look like there's a way for an activation hook to do anything except throw an error or generated "unexpected output". Am I missing something?

Is there a good way to refuse plugin activation from the activation hook?

3
  • 2
    you don't need to prevent activation to do this, you could instead perform this check before the rest of your plugin runs, print a message in WP Admin via the notices hooks, then return early so none of your plugins code runs. This way your plugin remains active, but doesn't do anything other than tell the user they need to install a PHP extension. This doesn't answer your question, but it does solve your problem
    – Tom J Nowell
    Nov 17, 2022 at 12:03
  • also note that this extension is a bundled with PHP 7.4 as a hard requirement, so this question doesn't make sense for any PHP version newer than that
    – Tom J Nowell
    Nov 17, 2022 at 12:04
  • Still about a third of WP sites are < 7.4. Sigh. wordpress.org/about/stats And it turns out that SQLite versions are all over the place.
    – O. Jones
    Nov 18, 2022 at 11:25

1 Answer 1

1

I use code similar to this to check for the minimum requirements for a plugin:

if (is_admin()) {
// check for required versions of WP and PHP
    $min_wp  = '4.9.6';
    $min_php = '7.2';
    if (!check_requirements($min_wp, $min_php)) {
        add_action('admin_init', 'my_disable_plugin');
        add_action('admin_notices', 'my_show_notice_disabled_plugin');
        add_action('network_admin_init', 'my_disable_plugin');
        add_action('network_admin_notices', 'my_show_notice_disabled_plugin');
        my_deregister();
        return;
    }
}
function check_requirements($min_wp, $min_php) {
   // see this for how to check requirements https://www.sitepoint.com/preventing-wordpress-plugin-incompatibilities/ 
   // set $requirements_met true if OK, false if not OK
return $requirements_met;
}

function my_disable_plugin() {
   // disable the plugin 
}
function my_show_notice_diabled_plugin() {
   // admin notice that plugin disabled
}
function my_deregister() {
   // de-register the plugin
}

So if on the admin page, the plugin minimum requirements are checked. If that fails, the plugin is disabled and de-registered, and the admin notices displayed with the alert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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