WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Returning an error with activate_plugin hook (6 posts)

  1. philipjohn
    Member
    Posted 1 year ago #

    Hi folks,

    I want to be able to prevent a plugin being activated when certain conditions are present.

    Looking through core it seemed to make sense to use the activate_plugin hook (line 540 of wp-admin/includes/plugin.php).

    The function I've hooked in checks for the conditions and through logging I've confirmed that the conditions are are all present. Then I return an error, like so;
    return new WP_Error('plugin_conditions', __('This plugin cannot be activated at this time.'));

    However, the plugin is activated successfully and no error is shown.

    So two questions;
    1) Do you think I'm using the right hook to check for my conditions?
    2) Why, when my conditions are present, does returning a new WP_Error not stop the plugin activation and show my error message?

    I notice a few lines below the activate_plugin hook (line 559) there is similar code to return a WP_Error object when unexpected output it generated. This obviously works and I'm not sure how my implementation differs.

    Thanks,
    Phil

  2. bcworkz
    Member
    Posted 1 year ago #

    Your error fails because it's not expected by the code receiving your returned error. The other returned error works because whatever called activate_plugin() has anticipated a possible error return.

    Anyway, you don't need this hook to do your checks, plugins can create their own special hooks to run on activation. See Function Reference/register activation hook

  3. philipjohn
    Member
    Posted 1 year ago #

    That's what I kinda figured... so I'm guessing if I changed my error code to 'unexpected_output' it'd probably work...?

    Anyway, you don't need this hook to do your checks, plugins can create their own special hooks to run on activation

    Sorry, I should have headed off that suggestion originally. My code is an a plugin that will be active and will check the conditions when OTHER plugins are active. It is not checking for those conditions upon it's own activation. Hence why I haven't used the activation hook. Thanks for suggesting it though!

  4. philipjohn
    Member
    Posted 1 year ago #

    so I'm guessing if I changed my error code to 'unexpected_output' it'd probably work...?

    No is the answer to that :)

  5. bcworkz
    Member
    Posted 1 year ago #

    OK, with that action, you basically have to handle your own errors, you could simply die(), or a redirect might work. Anything you return just gets pushed into an array of plugins with out checking the content.

  6. philipjohn
    Member
    Posted 1 year ago #

    I wanted to avoid die() just because I think it looks messy (and stops other plugins activating if activating more than one at a time...) but I guess that might be the way to do it.

    I do have one other idea that I'll try first though.

    Thanks for your help :)

Topic Closed

This topic has been closed to new replies.

About this Topic