Omer Omer - 1 year ago 112
PHP Question

Display message on plugin activation and deactivation in wordpress

I'm building a WordPress plugin and want to display a message on activating the plugin and on its deactivation.

It should display the message one time on the plugins page in the dashboard.

Plugin Message

I have tried it with

hook but the message continuously displayed.

Can anyone tell me any condition or method to achieve this or any WordPress hook?

Answer Source

Well you can show the message on activation of your plugin only once.

Here is how you can do it:

1) Register a new setting option in your plugin i.e. plugin_status

2) Write this code:

 function when_my_plugin_activate() {
       if (get_option('plugin_status') != 'active' && !is_plugin_active('your-plugin-folder/your-plugin-file.php')) {
           echo 'Your Message Here!';
           update_option('plugin_status', 'active');

add_action('admin_notices', 'when_my_plugin_activate');

3) Here is another code block on deactivation of the plugin to change the status:

function when_my_plugin_deactivate() {
    update_option('plugin_status', 'inactive');
register_deactivation_hook(__FILE__, 'when_my_plugin_deactivate');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download