Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
strangerstudios / my_pmpro_pre_handle_404.php
Last active September 4, 2018 14:34
If we 404, and the slug matches a pmpro discount code, redirect
/*
If we 404, and the slug matches a discount code, redirect
Add this code to a custom plugin
*/
function my_pmpro_pre_handle_404($preempt, $wp_query) {
global $wpdb;
//bail if PMPro is not installed
if(empty($wpdb->pmpro_discount_codes_levels))
@strangerstudios
strangerstudios / pmpro_queries_with_period_vars.sql
Created October 13, 2017 18:54
PMPro SQL Queries Comparing Sales From 2 Different Periods
SET @p1start = '2016-04-01';
SET @p1end = '2016-07-01';
SET @p2start = '2017-04-01';
SET @p2end = '2017-07-01';
#period 1
select CONCAT('Period 1, ', @p1start, ' to ', @p1end) AS '';
# new paid orders
SELECT COUNT(DISTINCT(mo1.id))
@strangerstudios
strangerstudios / pmpro_queries.sql
Last active November 2, 2021 13:05
Some queries around PMPro sales/etc
# successful paid orders for a given month
SELECT COUNT( * )
FROM wp_pmpro_membership_orders
WHERE
total > 0 AND
status NOT IN (
'error', 'token', 'refund', 'pending', 'review'
)
AND TIMESTAMP > '2017-10-01'
AND TIMESTAMP < '2017-11-01';
@strangerstudios
strangerstudios / pmpro_clear_enddates.sql
Last active April 8, 2021 18:32
Clear out end dates for active subscriptions since the subs are managed by the gateway.
# oops I had PMPro setup with recurring subscriptions AND expiration dates
# I really didn't need the expiration date because the subscription is managed by the gateway
# if payment fails, the gateway will try again based on its settings
# and when the gateway gives up, it will tell PMPro through IPN/webhook to cancel the subscription
# If I have expiration dates setup for recurring subscription, PMPro is going to cancel those subscriptions
# whether they pay or not. So I need to edit the level and remove the expiration date AND
# run this script to clear out the end dates for active subscriptions.
####
# BACK UP YOUR DATABASE FIRST
####
@strangerstudios
strangerstudios / my_pmpro_affiliatewp_after_change_membership_level.php
Last active April 12, 2021 22:27
Create and Manage Affiliates based on membership level when using AffiliateWP and Paid Memberships Pro
<?php
/*
Creates an affiliate for new members of Level ID 1.
Updates affiliate status if the affiliate already exists.
Sets affilaite status to 'inactive' if membership of Level ID 1 is cancelled (includes expiration).
Add this code to a custom plugin.
*/
function my_pmpro_affiliatewp_after_change_membership_level( $level_id, $user_id, $cancel_level ) {
//make sure affiliatewp is active
@strangerstudios
strangerstudios / my_pmpro_level_cost_text_for_ab_test.php
Created September 19, 2017 19:13
Change the level cost text of a PMPro level for an A/B test.
/*
Set a cookie when users enter a specific page.
(You would setup Google Experiments or your A/B testing software
to send 50% of visitors to this page.)
*/
function my_wp_set_cookie_for_ab_test() {
if(is_page('pricing-2')) {
setcookie('ab_test_group_2', 1, 0, COOKIEPATH, COOKIE_DOMAIN, false);
}
}
@strangerstudios
strangerstudios / pmpro_is_level_expiring_soon_yes_on_levels_page.php
Created September 13, 2017 14:21
Show a renew link instead of Your Level for recurring subscriptions on the levels page with Paid Memberships Pro.
/*
Show a renew link instead of Your Level
for recurring subscriptions on the levels page.
Add this code to a custom plugin.
*/
function pmpro_is_level_expiring_soon_yes_on_levels_page($expiring) {
//only adjust this on the levels page so we don't mess anything up
global $pmpro_pages;
if(!is_admin() && !empty($pmpro_pages) && is_page($pmpro_pages['levels']))
@strangerstudios
strangerstudios / pmpro_email_filter_checkout_from_users.php
Last active September 13, 2017 18:57
Have Paid Memberships Pro admin checkout emails come from the customer.
/*
Have admin checkout emails come from the customer.
Add this code to a custom plugin.
*/
function pmpro_email_filter_checkout_from_users($email)
{
if(strpos($email->template, 'checkout') !== false) {
//get user info
$user = get_user_by('login', $email->data['user_login']);
@strangerstudios
strangerstudios / mainenance_mode_on.php
Created August 31, 2017 11:36
Redirect users to a maintenance.html file unless they are coming from your IP address. WordPress
/*
Lock site down to just our IP
Create a file /maintenance.html that explains that maintenance mode is on.
Update your IP address in the line below.
Add this code to your active theme's functions.php or a custom plugin.
*/
function mainenance_mode_on() {
if($_SERVER['REMOTE_ADDR'] == 'YOURIPADDRESSHERE') //find your IP and edit here
return;
@strangerstudios
strangerstudios / my_custom_disable_admin_bar.php
Created August 30, 2017 22:14
Disable the admin bar for any user who is not admin, can't edit users and can't edit posts.
/*
Disable the admin bar for any user who is not admin,
can't edit users and can't edit posts.
Add this code to a custom plugin.
*/
function my_custom_disable_admin_bar() {
if(!current_user_can('administrator') && !current_user_can('edit_users') && !current_user_can('edit_posts')) {
add_filter( 'show_admin_bar', '__return_false' );
add_action( 'admin_print_scripts-profile.php', 'habfna_hide_admin_bar_settings' );
}