Skip to content

Instantly share code, notes, and snippets.

@DumahX
DumahX / maybe-exclude-protected-posts.php
Last active August 15, 2022 16:47
Excludes protected posts from WP search results
<?php
function maybe_exclude_protected_posts($query) {
if(!$query->is_admin && $query->is_search && $query->is_main_query()) {
$posts_to_exclude = array();
$posts = get_posts(array('numberposts' => -1));
foreach($posts as $post) {
if(MeprRule::is_locked($post)) {
$posts_to_exclude[] = $post->ID;
<?php
function mepr_restrict_email_domains($errors) {
# Allowed email domains.
$allowed_email_domains = array('gmail.com', 'hotmail.com');
$email_address = isset($_POST['user_email']) ? trim($_POST['user_email']) : '';
# Don't bother validating if the email is empty.
if(empty($email_address)) {
@DumahX
DumahX / mepr_validate_username_length.php
Created December 23, 2021 17:17
Check the member's username length.
<?php
function mepr_validate_username_length($errors) {
$username = isset($_POST['user_login']) ? trim($_POST['user_login']) : '';
# Don't bother validating username if isn't present.
if(!$username) {
return $errors;
}
<?php
function cleanup_abandoned_signups($event) {
$txn = $event->get_data();
$user = $txn->user();
// Count complete transactions and non-complete transactions
$user_incomplete_txns = $user->transactions('`status` != "complete"');
$user_complete_txns = $user->transactions('`status` = "complete"');
<?php
function subscription_stopped_func($event) {
$sub = $event->get_data();
$latest_txn = $sub->latest_txn();
if ($sub->in_trial() && $latest_txn) {
// Set transaction's expiration date to yesterday. Effectively revokes their membership access.
$latest_txn->expires_at = MeprUtils::ts_to_mysql_date(time() - MeprUtils::days(1));
$latest_txn->store();
<?php
// Attach a phone number custom field when creating a Stripe Customer object.
// Replace mepr_phone_number with the actual slug of the phone number custom field in MemberPress.
function mepr_stripe_customer_extended_args($args, $user) {
$phone_number = trim(get_user_meta($user->ID, 'mepr_phone_number', true));
if(!empty($phone_number)) {
$args['phone'] = $phone_number;
}
<?php if(!defined('ABSPATH')) {die('You are not allowed to call this page directly.');} ?>
<div class="mp_wrapper">
<?php if(!empty($welcome_message)): ?>
<div id="mepr-account-welcome-message">
<?php echo MeprHooks::apply_filters('mepr-account-welcome-message', do_shortcode($welcome_message), $mepr_current_user); ?>
</div>
<?php endif; ?>
<?php if( !empty($mepr_current_user->user_message) ): ?>
<?php
function mepr_validate_username($errors) {
$username = isset($_POST['user_login']) ? trim($_POST['user_login']) : '';
$first_name = isset($_POST['user_first_name']) ? trim($_POST['user_first_name']) : '';
$last_name = isset($_POST['user_last_name']) ? trim($_POST['user_last_name']) : '';
// If username is empty, don't run any validations.
if(empty($username)) {
return $errors;
<?php
function block_member_by_name($errors) {
$first_name = isset($_POST['user_first_name']) && !empty($_POST['user_first_name']) ? trim($_POST['user_first_name']) : '';
$last_name = isset($_POST['user_last_name']) && !empty($_POST['user_last_name']) ? trim($_POST['user_last_name']) : '';
// If first or last name is empty, just don't do anything.
if(empty($first_name) || empty($last_name)) {
return $errors;
}
<?php
/*
INSTRUCTIONS FOR THIS CODE:
On your RSS URL add &allow_feed=true to the end of it.
Then copy the below code and paste it into a plugin like My Custom Functions or Code Snippets on your site.
This will allow other plugins/sites to view the feed without the content blocks.
*/
function allow_feed_through($block, $post, $uri) {
if(isset($_GET['allow_feed'])) {