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();
}
}
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: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.
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:
update_option()
). You might want to create a settings page with a select drop-down that shows its enable state.get_option()
to disable the admin notice and continue processing your code.