Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();
    }
} 
share|improve this question
2  
You didn't actually ask a question... Is there some specific part you're having a problem with, or do you just not know where to start? I'd recommend reading the About page and FAQ to learn about the mechanics of how this site works. –  Ian Dunn Sep 14 '12 at 23:56
    
Edited. Thanks for the tip! –  Daniel Sep 15 '12 at 2:57
add comment

2 Answers

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.

share|improve this answer
    
Well I'm not a programmer, but I can see that your code really makes sense. I changed the "if (!$confirmed)" to "if (!in_array(FILE, $plugins))" and place my error message inside the function wpse65190_on_activation_note as you describe, but I'm getting a message "The plugin generated x characters of unexpected output"... What's wrong? =) –  Daniel Sep 15 '12 at 14:15
    
That's because of the echo. Just return instead. –  kaiser Sep 15 '12 at 18:39
    
I replace the echoes to returns, but get the same error message. =( –  Daniel Sep 17 '12 at 18:40
    
Then you got an error somewhere else. Make sure to turn on every WP and php error display function. This message only appears if something echos (or throws an error) on activation. I'm 110% sure. –  kaiser Sep 17 '12 at 19:57
    
You'r right! I found a function that was causing the error. Now the error is gone, but the admin message still not appear. My debug file show this error: "PHP Warning: call_user_func_array() [<a href='function.call-user-func-array'>function.call-user-func-array</a>]: First argument is expected to be a valid callback, 'on_activation' was given in /home/..." –  Daniel Sep 18 '12 at 19:04
show 6 more comments

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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