Skip to content

Instantly share code, notes, and snippets.

View ideadude's full-sized avatar

Jason Coleman ideadude

View GitHub Profile
@ideadude
ideadude / PMPro_Membership_Level.php
Created September 30, 2022 18:53
Some example code of how to create a PMPro membership level.
<?php
/**
* Some example code of how to create a PMPro membership level.
* Uses the PMPro_Membership_Level class and save() method.
*/
// Copy a level.
$level = new PMPro_Membership_Level( 1 ); // Pass the ID of the level to copy.
$level->id = $level->ID = null; // Clear out the ID, triggers insert on save.
$level->name = 'NewName'; // Update any property you want.
@ideadude
ideadude / non_featured_images_query.sql
Created September 20, 2022 19:12
SQL Query to find images (and other attachments) that aren't being used as featured images.
@ideadude
ideadude / add-billing-to-add-member-and-profile.php
Last active August 25, 2022 17:31 — forked from andrewlimaza/add-billing-to-add-member-and-profile.php
Add billing fields to Add Member Settings
<?php
/*
* Add this code below to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
* This will add billing fields to Add Member and the user's profile page.
*/
function add_billing_fields_to_add_member_profile() {
//check for register helper
if(!function_exists("pmpro_add_user_field")) // pmprorh_add_registration_field
return;
@ideadude
ideadude / my_update_location_options.php
Last active August 23, 2022 20:18
Update a PMPro User Field with the pmpro_add_user_field filter hook.
<?php
/**
* Update a PMPro User Field with Code.
* Requires PMPro 2.9.3+
*/
function my_update_location_options( $field ) {
global $pmpro_countries;
// This filter runs early, so need to load this.
require_once( PMPRO_DIR . '/includes/countries.php' );
@ideadude
ideadude / my_update_location_options.php
Created August 23, 2022 13:40
Example of how to update a PMPro user field with code.
<?php
/**
* Update a PMPro User Field with Code.
* We find the home_country field and update the options.
* User fields are loaded into the pmpro_user_fields globals
* during the init hook, priority 1. So we just need to run after that.
*/
function my_update_location_options() {
global $pmpro_user_fields, $pmpro_countries;
@ideadude
ideadude / my_approve_user_if_already_approved.php
Created February 18, 2022 22:11
PMPro Approvals: If a user was approved for any other level, consider them approved for every level.
<?php
/**
* If a user was approved for any other level, consider them approved for every level.
* Requires the PMPro Approval Process for Membership Add On - https://www.paidmembershipspro.com/add-ons/approval-process-membership/
* Must be using PMPro Approvals version 1.4.2 or higher.
* Add this code to your site following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_approve_user_if_already_approved( $approved, $user_id, $level_id, $user_approval ) {
@ideadude
ideadude / pmpro_membership_content_filter_debug.php
Created February 9, 2022 21:31
Debug PMPro's pmpro_membership_content_filter() function.
<?php
/**
* We can use the pmpro_membership_content_filter filter in
* the pmpro_membership_content_filter() function to debug
* and see what the $content and $hasaccess vars are set to
* at the time of running.
*
* IMPORTANT: This code will effectively break your site with
* the debug info. Only use this for a moment, then deactivate it.
*
@ideadude
ideadude / my_pmpro_cancel_levels_one_by_one
Last active February 8, 2022 14:13
When cancelling "all levels" in PMPro, do them one at a time so CONPD works.
<?php
/**
* This gist updates the "cancel all memberships" link on the cancel page
* to pass the level ids into the page instead of "all".
* This will ensure that all the levels are cancelled one by one.
* In addition to sending a separate email for each cancellation,
* this also ensures the $old_level parameter is sent to the
* pmpro_cancelMembershipLevel() function so Add Ons like the
* Cancel on Next Payment Date one continue to work.
*
@ideadude
ideadude / my_keep_members_only_products_out_of_carts.php
Created January 6, 2022 14:54
Prevent non-members from adding member products to their cart with WooCommerce and PMPro.
<?php
/**
* Prevent non-members from adding member products to their cart.
* This requires the PMPro CPT Add On be installed and
* each product should be checked for which levels are required.
*/
function my_keep_members_only_products_out_of_carts( $is_purchasable, $product ) {
// Not purchasable for some other reason.
if( ! $is_purchasable ) {
return $is_purchasable;
@ideadude
ideadude / my_hide_members_only_products_from_archives.php
Created January 6, 2022 14:53
Also hide members-only "products" CPT from archives.
<?php
/**
* Also hide members-only "products" CPT from archives.
* You must use the PMPro CPT Add On and then check a produc to require membership.
* And also set the "Filter searches and archives?" option to Yes in the PMPro advanced settings.
*/
function my_hide_members_only_products_from_archives( $post_types ) {
if ( ! in_array( 'product', $post_types ) ) {
$post_types[] = 'product';
}