So, you want to collect feedback from your plugin users whenever they decide to deactivate your plugin? As plugin developers, feedback matters the most in improving a plugin and adding more rich and most wanted features.

The aim of this tutorial is to add a quick survey to the admin panel which will show whenever a client tries to deactivate our plugin.

It doesn’t mean it’ll break the deactivation process, but it will nicely as the user if they want to submit a quick and short survey as to why they’re deactivating our precious plugin. And if ever they decide not to do that, they can just use the link for that, or submit an empty survey.

Things we’ll be using in this tutorial:

  • admin_init – to hook us into the admin.
  • wp_die – to display our deactivation notice and survey form when our plugin is about to be deactivate.
  • wp_mail – to send me (the plugin developer) an email with the survey result, which gets sent upon survey submission.

Here’s the full tutorial’s code:

/**
  * Add survey to plugin deactivation
  *
  * @author Samuel Elh <samelh.com/contact>
  * @see https://samelh.com/blog
  */
function my_plugin_deactivation_survey() {
    /**
      * Your plugin basename
      *
      * You can make this dynamic and use plugin_basename( PLUGIN_FILE )
      * @see https://codex.wordpress.org/Function_Reference/plugin_basename
      *
      * bbpress is just a placeholder here
      */
    $plugin = 'bbpress/bbpress.php';

    // making sure we're running in plugins.php admin page
    if ( !isset($GLOBALS['pagenow']) || 'plugins.php' != $GLOBALS['pagenow'] )
        return;

    // deactivations only
    if ( !isset($_REQUEST['action']) || 'deactivate' != $_REQUEST['action'] )
        return;

    // run on our plugin deactivation
    if ( !isset($_REQUEST['plugin']) || $plugin != $_REQUEST['plugin'] )
        return;

    // make sure we don't make plugin deactivations impossible
    if ( isset($_REQUEST['ignore_sv']) )
        return;

    if ( isset($_POST['submit_sv']) ) {
        $reason = !empty($_POST['reason']) ? ( 'other' != $_POST['reason'] ? trim($_POST['reason']) : (
            !empty($_POST['other']) ? trim($_POST['other']) : null
        ) ) : null;

        if ( $reason ) {
            /**
              * Here we forward an email to our inbox as plugin authors.
              * You can include more technical info about the user and the environment
              * such as PHP_VERSION, get_bloginfo('version'), is_multisite(), etc
              * but make sure you do that with user consent.
              */
            wp_mail('[email protected]', 'Deactivation Survey', sprintf(
                'Deactivation reason for "%s": %s',
                $plugin, $reason
            ));
        }

        // refresh the page to continue to plugin deactivation
        wp_safe_redirect( add_query_arg( 'ignore_sv', 1 ) );
        exit;
    } else {
        ob_start(); ?>

        <h2>Deactivation Survey</h2>
        <form method="post">
            <p>We see you are about to deactivate our plugin. Do you mind taking a quick survey?</p>
            <p>
                <a href="javascript:;" id="showhide">Sure! Show me</a> |
                <a href="<?php echo add_query_arg( 'ignore_sv', 1 ); ?>">Nah, thanks, proceed to deactivation</a>
            </p>
            <div id="sv" style="display:none">
                <p>Thank you! Why you are deactivating this plugin?</p>
                <label style="display:block"><input type="radio" name="reason" value="Reason 1"> Reason 1</label>
                <label style="display:block"><input type="radio" name="reason" value="Reason 2"> Reason 2</label>
                <label style="display:block"><input type="radio" name="reason" value="other"> Another reason</label>
                <p id="other" style="display:none">
                    <input type="text" name="other" />
                </p>
                <p><input type="submit" name="submit_sv" value="Submit Survey and Deactivate Plugin" />
            </div>
        </form>

        <script type="text/javascript">
        /** it's safe to override window.onload here as there's 
          * no client JS running on the errors page */
        window.onload = function()
        {
            var showhide = document.getElementById('showhide')
              , reason = document.querySelectorAll('[name="reason"]');
            if ( null != showhide ) showhide.onclick = function()
            {
                var e = document.getElementById('sv');
                e.style.display=(e.style.display?'':'none');
            }
            if ( reason.length ) {
                var other = document.getElementById('other');
                for ( var c in reason ) {
                    if ( reason.hasOwnProperty(c) ) reason.onchange = function()
                    {
                        if ( 'other' == this.value ) {
                            other.style.display = '';
                            other.children[0].focus();
                        } else {
                            other.style.display = 'none';
                        }
                    }
                }
            }
        }
        </script>

        <?php wp_die(ob_get_clean(), 'Deactivating plugin..' );
    }
}

add_action('admin_init', 'my_plugin_deactivation_survey');

That can be added somewhere in your plugin. For the sake of simplicity, I have used simple form fields with simple answers as well, but you can always change that and add your own HTML and design it the way you want, and don’t forget to change the JavaScript as well.

You should change bbpress/bbpress.php plugin with your own plugin file, you can retrieve this for your current plugin using the plugin basename function.

You may not want to rely on wp_mail, you may have some endpoint URL somewhere around your site which you can ping instead with the survey outcome using wp_remote_get or wp_remote_post, it is all up to you.

wp_mail('[email protected]', 'Deactivation Survey', sprintf(
    'Deactivation reason for "%s": %s',
    $plugin, $reason
));

The survey email body is simple, it does not include any client or environment technical details, only the survey reason, but you can attach more technical info to your survey, something that would help your quest:

  • PHP_VERSION to know which PHP version the user is running.
  • get_bloginfo('version') to get the current WordPress core version.
  • Your plugin version.
  • is_multisite() to know if the user is running your plugin on multisite or just a regular WP installation.
  • etc …

Make sure you respect your clients privacy and let them know about the information you are collecting.

Digital Ocean

Cheap Cloud SSD Hosting

Get a VPS now starting at $5/m, fast and perfect for WordPress and PHP applications

Sign Up with $10 Credit