Show a confirm message before plug-in activation

I want to display a message when the user try to activate my plugin. This message will ask if him really wants to activate the plugin or, if him change his mind, cancel the activation. How can I accomplish that?
This is the code for the warning message just for reference.

— UPDATED CODE —

register_activation_hook( __FILE__, 'on_activation' );

function on_activation() {
   // Add the admin notice:
   add_action( 'admin_notices', 'on_activation_note' );

   // Then you should check the DB option: 
   $plugins = get_option( 'active_plugins' );

   // Do all the checks from the confirmation message
   if ( !in_array(__FILE__, $plugins) ) {
   unset( $plugins[ dirname( __FILE__ ) ] );
   update_option( 'active_plugins', $plugins );
   }
}

function on_activation_note() {
global $pagenow;
if ( $pagenow == 'plugins.php' ) {
    ob_start(); ?>
    <div id="message" class="error">
    <p><strong>Aviso</strong></br>
    Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.</p>
    <p><span><a class="button" href="">Cancelar</a></span>
    <span><a class="button" href="">Continuar</a></span></p>
    </div>
    <?php
    echo ob_get_clean();
    }
} 

Related posts

Leave a Reply

2 comments

  1. You can read more about the details of activation on this answer.

    Basically you need to hook a function to register_activation_hook() – assuming, that this is from within your main plugin folder and not a subfolder:

    register_activation_hook( __FILE__, 'on_activation' );
    function wpse65190_on_activation()
    {
       // Add an admin notice:
       add_action( 'admin_notices', 'wpse65190_on_activation_note' );
    
       // Then you should check the DB option: 
       $plugins = get_option( 'active_plugins' );
    
       if ( ! in_array( dirname( __FILE__ ), $plugins )
       {
           unset( $plugins[ dirname( __FILE__ ) ] );
           update_option( 'active_plugins', $plugins );
       }
    }
    function wpse65190_on_activation_note()
    {
        // Add your note here: Maybe a form?
    }
    

    It’s as easy as that. You just have to fill in the gaps. If you got a full working example, please update this answer with your working code. Thanks.

  2. You need to create two distinctions activated and enabled. You HAVE TO have your plugin activated AKA included for WP to know what logic to process. Even though the plugin is activated, you can still disable functionality and alert the user that the plugin is activated, but not yet enabled.

    There are several plugins that use a secondary step of enabling. You can take a look at Private WP and All-In-One SEO Pack to see how they accomplish this.

    Here’s how I would approach it:

    1. Set an admin notice once the plugin is activated alerting the user they need to enable the functionality first.
    2. If a certain query string is set, add an option that notes they’ve elected to enable the functionality (i.e. update_option()). You might want to create a settings page with a select drop-down that shows its enable state.
    3. Check that option using get_option() to disable the admin notice and continue processing your code.