WordPress

Restrict Content Pro Stats WordPress Dashboard Widget

If you use Restrict Content Pro on your WordPress membership site then you most likely find yourself logging in regularly to check the stats on your site.

One of the bits of information I want to see when using a membership site is how many active users did I get today, how many people cancelled and what is the retention rate with members of the site.

In this tutorial we're going to build a new WordPress dashboard widget which will quickly tell us information of our members when using Restrict Content Pro plugin.

restrict-content-pro-dashboard

What Are WordPress Dashboard Widgets

WordPress dashboard widgets are sections of code that is display on your WordPress admin area home page. They are useful ways of displaying quick bits of information about your WordPress site such as amount of posts, comments, plugin information.

This is why it's the perfect location to have quick information for our membership site to see certain stats for our new members.

If you want to remove any of the default WordPress dashboard widgets follow this tutorial.

Create Your Own WordPress Dashboard

Let's create a new plugin which will add this dashboard to our site. First create a folder in your plugin directory and add a new plugin file with the following plugin comments.

/**
 * Plugin Name: Paulund Restrict Content Pro Dashboard
 * Plugin URI: http://www.paulund.co.uk
 * Description: Creates a dashboard widget to view stats from restrict content pro
 * Version: 1.0
 * Author: Paulund
 * Author URI: http://www.paulund.co.uk
*/

This creates a new WordPress plugin we can add our code to display the stats on our members.

To create a new WordPress dashboard widget you need to run the function wp_add_dashboard_widget on the wp_dashboard_setup action. The below code is a quick way of how you will add the dashboard widget code to your site.

class Paulund_Restrict_Content_Pro_Dashboard
{
    private $rcpStats = false;

    /**
     * Create a new WordPress widget
     */
    public function __construct()
    {
        add_action('wp_dashboard_setup', array($this, 'add_new_rcp_dashboard' ));
    }

    /**
     * Add a new WordPress widget
     */
    public function add_new_rcp_dashboard()
    {
        /**
         * Widget Id
         * Widget Name
         * Function
         */
        wp_add_dashboard_widget('rcp_dashboard', 'Restrict Content Pro Dashboard', array($this, 'rcp_dashboard_widget_function'));
    }

    /**
     * Display the rcp dashboard widget with stats
     *
     * @param $post
     * @param $callback_args
     */
    public function rcp_dashboard_widget_function( $post, $callback_args )
    {
        // Display code for your dashboard widget here
    }
}
new Paulund_Restrict_Content_Pro_Dashboard();

We've left the rcp_dashboard_widget_function blank for now as this will be used to display the stats on the widget.

Get User Stats

This section we're going to get the stats for the different members on our site, the following is the total information we need.

  • New active users today
  • Total active users
  • Total pending users
  • Total expired users
  • Total cancelled users
  • Total retention

On all of the following queries of the users we are going to use the WP_User_Query class to get all the information that we need.

New active users today

To get the active users of today we need to use a date_query to get users which have registered in the last 24 hours. Then we need to search for users which have a meta_key of rcp_status with a meta_value of active.

/**
     * Get the total active users in the last 24 hours
     *
     * @return int
     */
    public function getTodayActive()
    {
        $args = array(
            'date_query' => array(
                array( 'after' => '24 hours ago', 'inclusive' => true )
            ),
            'meta_key' => 'rcp_status',
            'meta_value' => 'active',
        );

        $userQuery = new WP_User_Query($args);

        return $userQuery->get_total();
    }

Total active users

To get the total active users we need to search for users which have a meta_key of rcp_status and a meta_value of active.

/**
     * Get the total active users
     *
     * @return int
     */
    public function getTotalActive()
    {
        $args = array(
            'meta_key' => 'rcp_status',
            'meta_value' => 'active',
        );

        $userQuery = new WP_User_Query($args);

        $this->totalActive = $userQuery->get_total();

        return $this->totalActive;
    }

Total pending users

To get the total pending users we need to search for users which have a meta_key of rcp_status and a meta_value of pending.

/**
     * Get the total pending users
     *
     * @return int
     */
    public function getTotalPending()
    {
        $args = array(
            'meta_key' => 'rcp_status',
            'meta_value' => 'pending',
        );

        $userQuery = new WP_User_Query($args);

        return $userQuery->get_total();
    }

Total expired users

To get the total expired users we need to search for users which have a meta_key of rcp_status and a meta_value of expired.

/**
     * Get the total expired users
     *
     * @return int
     */
    public function getTotalExpired()
    {
        $args = array(
            'meta_key' => 'rcp_status',
            'meta_value' => 'expired',
        );

        $userQuery = new WP_User_Query($args);

        $this->totalExpired = $userQuery->get_total();

        return $this->totalExpired;
    }

Total cancelled users

To get the total cancelled users we need to search for users which have a meta_key of rcp_status and a meta_value of cancelled.

/**
     * Get the total cancelled users
     *
     * @return int
     */
    public function getTotalCancelled()
    {
        $args = array(
            'meta_key' => 'rcp_status',
            'meta_value' => 'cancelled',
        );

        $userQuery = new WP_User_Query($args);

        $this->totalCancelled = $userQuery->get_total();

        return $this->totalCancelled;
    }

Total retention

To get the total retention of users we need to work out from the amount of total active users compared to the amount of users that cancelled and get a percentage value of that stats.

/**
     * Get the retention stats
     *
     * @return int
     */
    public function getRetention()
    {
        return $this->totalActive / (($this->totalActive + $this->totalExpired + $this->totalCancelled) / 100 );
    }

Display The Stats Table

Now we have the code to get all the stats for the different users we can now go back to the dashboard widget and place the code to display the HTML dashboard widget which is simply going to be a table with the widget information.

/**
     * Display the rcp dashboard widget with stats
     *
     * @param $post
     * @param $callback_args
     */
    public function rcp_dashboard_widget_function( $post, $callback_args )
    {
        ?>
        <style>
            .paulund_rcp_dashboard
            {
                width: 100%;
            }
        </style>
        <table class="paulund_rcp_dashboard">
            <tr>
                <th colspan="2"><strong>Daily</strong></th>
            </tr>
            <tr>
                <td>Active</td>
                <td><?php echo $this->getRcpStats()->getTodayActive(); ?></td>
            </tr>
            <tr>
                <th colspan="2"><strong>Total</strong></th>
            </tr>
            <tr>
                <td>Active</td>
                <td><?php echo $this->getRcpStats()->getTotalActive(); ?></td>
            </tr>
            <tr>
                <td>Pending</td>
                <td><?php echo $this->getRcpStats()->getTotalPending(); ?></td>
            </tr>
            <tr>
                <td>Expired</td>
                <td><?php echo $this->getRcpStats()->getTotalExpired(); ?></td>
            </tr>
            <tr>
                <td>Cancelled</td>
                <td><?php echo $this->getRcpStats()->getTotalCancelled(); ?></td>
            </tr>
            <tr>
                <td>Retention</td>
                <td><strong><?php echo number_format($this->getRcpStats()->getRetention(), 2); ?>%</strong></td>
            </tr>
        </table>
        <?php
    }
Back to top

Members Download

Leave a Reply

Your email address will not be published. Required fields are marked *