Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Change how codes are generated for orders in Paid Memberships Pro. Add this code to your active theme's functions.php or a custom plugin.
Set random order "code" to equal the order ID
It is important to keep the prefix (PMPRO-) below or change it to something else with a non-number in it.
If a code is all numeric, PMPro will go into an infinite loop when a new order is created.
function my_pmpro_random_code($code, $order)
global $wpdb;
$prefix = 'PMPRO-';
//already have an id for this order? or should we guess what the next id is going to be?
$code = $prefix . $order->id;
else {
//get id of most recent order
$last_order_id = intval($wpdb->get_var('SELECT id FROM $wpdb->pmpro_membership_orders ORDER BY id DESC LIMIT 1'));
//increment and check if it's available until we find one, this is probably goin
$already_used = true;
while($already_used != false) {
$code = $prefix . $last_order_id;
$already_used = $wpdb->get_var("SELECT code FROM $wpdb->pmpro_membership_orders WHERE code = '" . esc_sql($code) . "' LIMIT 1");
return $code;
add_filter("pmpro_random_code", "my_pmpro_random_code", 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.