Skip to content

Instantly share code, notes, and snippets.

@cartpauj
cartpauj / mepr-force-first-name-display-name.php
Created April 3, 2017 18:34
Force a user's first name to be their WordPress display name (for MemberPress Users)
<?php
// ---------------------------------------------
// FORCE THE DISPLAY_NAME USER VARIABLE TO BE THEIR FIRST NAME
// ---------------------------------------------
function rol_set_display_name( $display_name ) {
// New user via WP Admin > Users > Add New
// Edit user's name via WP Admin > Users > edit user
// Edit user's name via WP Admin > MemberPress > Members > edit user
if ( isset( $_POST['first_name'] ) ) {
@cartpauj
cartpauj / mepr-show-account-nav-shortcode.php
Last active February 29, 2024 02:27
Shortcode to show MemberPress Account page navigation menu on any page
<?php
//
// PASTE SHORTCODE [mepr-account-nav-menu] after adding this snippet to your site
// Use Code Snippets plugin (run snippet only on front end)
// https://wordpress.org/plugins/code-snippets/
//
function mepr_show_nav_menu() {
if(!class_exists('MeprAccountCtrl')) { return; }
$mepr_options = MeprOptions::fetch();
@cartpauj
cartpauj / mepr-sub-actions.php
Created December 15, 2015 17:44
MemberPress Subscription Actions
<?php
// 1) User purchases and payment is complete
function capture_completed_transaction($txn) {
//It's possible this could be a recurring transaction for a product the user is already subscribed to so probably use a user meta field described below
$user = new MeprUser($txn->user_id); //A MeprUser object
$membership = new MeprProduct($txn->product_id); //A MeprProduct object
$users_memberships = $user->active_product_subscriptions('ids'); //An array of membership CPT ID's
//Here you may want to grab a user meta field of your own creating and check to see if the user is already subscribed to this membership or not
//If not, then push them to your 3rd party application and update the user meta field
<?php
/*
Plugin Name: MemberPress Account Page Nav
Plugin URI: http://www.memberpress.com/
Description: Allows developers to add more nav menu links/pages to their members account page
Version: 1.0.0
Author: Caseproof, LLC
Author URI: http://caseproof.com/
Copyright: 2004-2013, Caseproof, LLC
*/
@cartpauj
cartpauj / mepr-ecommerce.php
Last active January 17, 2024 22:11
Google eCommerce Tracking for MemberPress Thank You Pages
<?php
//PASTE THIS CODE IN A PLUGIN LIKE My Custom Functions (RECOMMENDED) OR IN YOUR THEME'S functions.php FILE
//THIS ASSUMES YOU HAVE THE GOOGLE ANALYTICS JAVASCRIPT INCLUDED ALREADY
function echo_ga_tracking_script() {
if(isset($_GET['membership']) && isset($_GET['trans_num'])) {
$txn = MeprTransaction::get_one_by_trans_num($_GET['trans_num']);
if(isset($txn->id) && $txn->id > 0) {
//Echo the script to the HTML <head>
?>
@cartpauj
cartpauj / mepr-limit-countries-shown.php
Created April 1, 2020 16:52
Limit which countries are shown in the MemberPress Countries address dropdown fields at signup
<?php
// Paste the code below into a plugin like Code Snippets (run on front end snippet type)
function leave_one_country($countries, $prioritize_my_country) {
return array(
'DE' => _x('Germany', 'ui', 'memberpress')
);
}
add_filter('mepr_countries', 'leave_one_country', 10, 2);
@cartpauj
cartpauj / memberpress-corporate-all-sub-accounts.php
Last active January 1, 2024 05:55
Get all sub-accounts for a parent user in MemberPress Corporate Accounts
<?php
$user = MeprUtils::get_currentuserinfo();
$sub_user_ids = array();
if($user !== false) {
$transactions = $user->active_product_subscriptions('transactions');
if(!empty($transactions)) {
foreach($transactions as $txn) {
if(($sub = $txn->subscription()) !== false) {
@cartpauj
cartpauj / memberpress-disable-auto-login.php
Created June 12, 2018 21:49
Disable MemberPress auto-login
<?php
function mepr_disable_auto_login($auto_login, $membership_id, $mepr_user) {
return false;
}
add_filter('mepr-auto-login', 'mepr_disable_auto_login', 3, 3);
@cartpauj
cartpauj / memberpress-hooks.php
Last active July 20, 2023 17:46
Various Subscription and Transaction status hooks for MemberPress with some helpful comments
<?php
//Capture a new member signup. Only ever triggers once for each new member.
//Does not trigger for exising members who have subscribed to a Membership before.
//
//The user may not be logged in when this is called
//as it is triggered when a user is added over the REST API,
//and also when a user is added from the dashboard (MemberPress -> Members -> Add New)
function mepr_capture_new_member_signup_completed($event) {
$user = $event->get_data();
$txn_data = json_decode($event->args);
@cartpauj
cartpauj / nua-bypass-by-membership.php
Created June 13, 2023 14:07
Bypass New User Approve if certain Membership(s) is purchased in MemberPress
<?php
function nua_allow_user_if_membership($errors) {
$allowed_memberships = array(123,321,789,987); // CHANGE THIS
if(!is_user_logged_in()) {
if(in_array((int)$_POST['mepr_product_id'], $allowed_memberships)) {
$_REQUEST['action'] = 'createuser'; //Set this so "New User Approve" plugin will not limit the subscriber
}
}