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();
}
}
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.