3

Is there a specific Wordpress API way to ask the user's permission for something when activating a plugin?

Due to the Wordpress plugin directory policy I have to ask the users consent for the backlink in my widget, and I have to do it in my case before activating the plugin/widget.

My plugin registration is this

add_action('widgets_init', create_function('', 'return register_widget("ow_ptimes_plugin");'));

class ow_ptimes_plugin extends WP_Widget {
    function ow_ptimes_plugin() {
        parent::WP_Widget(false, 'Owlish times', array('description' => 'Widget to display times.'));
    }

So practically I just need a popup asking the user, at "yes" activating the plugin, at "no" canceling activation.

(this only for background info, my main question is wether this can be done via API. I have looked in the Codex but I did not found anything so far)

5
  • 2
    You might be able to create a popup using the activation hook:codex.wordpress.org/Function_Reference/register_activation_hook. Instead of a popup, you could use a site option, for example, a checkbox in Settings.
    – shanebp
    Oct 15, 2014 at 13:54
  • You can do it "before activating the plugin" using magic. To WordPress being able to run some code from your plugin, your plugin must be active. How do you can check permission to activate a plugin using code from a deactivated plugin? register_activate_hook run immediately after a plugin file has been loaded, so you need to output a fatal error (or die) to stop plugin activation, it means you can't render a popup from there. You have to add a plugin option page where the user can consent or not to add backlink.
    – gmazzap
    Oct 15, 2014 at 17:48
  • 2
    Also may I ask why are you using create_function instead of a closure? To stay 5.2 compatible? Only zombies and phantoms still use that version.. Also you really need to be 5.2 compatible, use a standard named function, it's fairly better, especially for ones that have modern PHP and that you are penalizing in favor of those still using 5.2
    – gmazzap
    Oct 15, 2014 at 18:16
  • 1
    You could set an option somewhere that states whether the user has agreed or disagreed. Simple true/false option value. Build your plugin in a way which it relies on your option value being true. If it's false, don't provide any further functionality of your plugin to the user. You could have a settings page, admin notice, or even a popup if so desired. But in order for that to work, your plugin needs to be activated. Your plugin is activated, but doesn't do anything until the user has been prompted to agree/disagree to your terms of use. Once agreed, provide full functionality. Oct 16, 2014 at 14:56
  • @G.M.thanks, I am new at wp development, still a lot to learn. Can you provide me a link about "closure"? Thanks
    – Owl
    Oct 17, 2014 at 10:34

1 Answer 1

-1

Try the following code:

add_action( 'admin_head', 'ask_for_activation' );

function ask_for_activation() {
    ?>
    <script type="text/javascript">
        jQuery(function($){
            $('span.activate a').click(function(e){
                var c = confirm('Are you sure wnat to activate?')
                if(!c) {
                    e.preventDefault();
                    return false;
                }
                return true;
            });
        });
    </script>
    <?php
}

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

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.