Automatically Downgrade MemberMouse Users with Python

MemberMouse is a premium WordPress plugin for creating your own membership site. Usually you have a free member status and various premium member statuses depending on their subscription level. When a premium user’s membership expires, MemberMouse does not automatically downgrade that user’s membership status to the free tier. They do show you how to do it though, with a custom script.

Francesco from Yoga n’ Ride asked me if I could create a solution for him to automatically downgrade expired premium memberships to the free tier. I decided on using python because it was much faster and didn’t require going through every single member using the MemberMouse API.

Francesco uses a Digital Ocean VPS running Ubuntu 16.04 so I could easily install the necessary python modules to interact with the MySQL database and the MemberMouse API.

Note: Please test this thoroughly on a staging server before implementing in production!

Automatically Downgrade MemberMouse Users with Python

Install the necessary Python dependencies for MySQL and the python modules using pip.

sudo apt-get update
sudo apt-get install libmysqlclient-dev python-pip python-dev
pip install python-mysql requests

Create the python script folder and script file

mkdir -p ~/scripts
nano ~/scripts/

Paste the python script below to automatically downgrade MemberMouse users when their membership expires.

You will need your WordPress MySQL credentials from wp-config.php.

You will need your MemberMouse API key and secret (KB article).

You will also need the free membership level IDs (KB article).

The script scans your database for your member IDs with expired memberships and uses the MemberMouse API to downgrade the user to the specified free membership level – here it is ID 3.

#!/usr/bin/env python

import requests
import MySQLdb

#WordPress MySQL credentials
dblocation = 'localhost'
dbuser = 'wordpress-sql-user'
dbpass = 'wordpress-sql-pass'
dbname = 'wordpress-sql-db'

#connect to the WordPress database
db = MySQLdb.connect(dblocation,dbuser,dbpass, dbname )

# prepare a cursor object using cursor() method
cursor = db.cursor()

#this gets all member IDs that are paying from the database
# status 2 is cancelled, get all paying users not in free plans with cancelled memberships
query = 'select wp_user_id from mm_user_data where status = "2" AND membership_level_id NOT IN '+freeids

# execute SQL query using execute() method.

# Fetch a single row using fetchone() method.
result = cursor.fetchall()

# disconnect from server

# membermouse api credentials
apikey = 'membermouse-api-key'
apisecret = 'membermouse-api-secret'
#id to downgrade expired members to, should be free membership id
membership_level_id = '3'

#set count variable

#loop through results, check if cancelled and downgrade
for member_id in result:
    data = {'apikey': apikey, 'apisecret': apisecret, 'member_id': member_id, 'membership_level_id': membership_level_id}
    dictionary ='/wp-content/plugins/membermouse/api/request.php?q=/updateMember', data=data)
    count += 1

print count

Give it a test run

python ~/

You will get some output about how many users were downgraded.

Schedule MemberMouse to Automatically Downgrade Members

Using a cronjob we can set the script to run automatically

crontab -e

Add this line while replacing username with your current Linux user and the script will run once a day.

@daily /usr/bin/python /home/username/scripts/

Ctrl+X, Y and Enter to Save and Exit.

Now your python script will run every day and automatically downgrade MemberMouse premium members to free members if their membership has expired.

You should consider setting up a MemberMouse push notification so users will be emailed if their membership expires.