Paid Memberships Pro

Add GST

<?php
/**
 * Add Australian GST to your membership site
 *
 * title: Custom tax structure where all levels except level 1 have 7.25% tax if billing state is CA.
 * layout: snippet
 * collection: checkout
 * category: tax, AU
 * link: https://www.paidmembershipspro.com/australian-gst/
 *
 * You can add this recipe to your site by creating a custom plugin
 * or using the Code Snippets plugin available for free in the WordPress repository.
 * Read this companion article for step-by-step directions on either method.
 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
 */

/*  
  Tax solution for Australia
  
  This solution assume the GST tax rate of 10% from March 15th, 2017. Ask your accountant how much tax you must charge.
  More info: https://www.business.gov.au/info/run/tax/register-for-goods-and-services-tax-gst
  
  Edit as needed, then save this file in your plugins folder and activate it through the plugins page in the WP dashboard.
*/ 

//add tax info to cost text. this is enabled if the Australia checkbox is checked.
function agst_pmpro_tax( $tax, $values, $order ) {
	$tax = round( ( float )$values['price'] * 0.1, 2 );
	return $tax;
}
//add tax info to cost text.
function agst_pmpro_level_cost_text( $cost, $level ) {
	//only applicable for levels > 1
	$cost .= __( ' Customers in Australia will be charged a 10% GST.', 'pmpro-australia-gst' );
	return $cost;
}
add_filter( 'pmpro_level_cost_text', 'agst_pmpro_level_cost_text', 10, 2 );
 
//set the default country to Australia
function agst_pmpro_default_country( $country ) {
	return 'AU';
}
add_filter( 'pmpro_default_country', 'agst_pmpro_default_country' );
 
//add AU checkbox to the checkout page
function agst_pmpro_checkout_boxes() {
?>
	<div class='pmpro_card'>
		<div class='pmpro_card_content'>
			<legend class='pmpro_form_legend'>
				<h2 class='pmpro_form_heading pmpro_font-large'>
					<?php _e( 'Australian Residents', 'pmpro-australia-gst' );?>
				</h2>
			</legend>
			<div id='pmpro_pricing_fields'>
				<input id='taxregion' name='taxregion' type='checkbox' value='1'
				<?php if( !empty( $_REQUEST['taxregion'] ) || !empty( $_SESSION['taxregion'] ) ) { ?>
				checked='checked'<?php } ?> />
				<label for='taxregion' class='pmpro_normal pmpro_label-inline pmpro_clickable'>
					<?php _e( 'Check this box if your billing address is in Australia.', 'pmpro-australia-gst' );?>
				</label>
			</div>
		</div>
	</div>
<?php
}
add_action( 'pmpro_checkout_boxes', 'agst_pmpro_checkout_boxes' );
 
//update tax calculation if buyer is Australian
function agst_pmpro_region_tax_check() {
	//check request and session
	if ( isset( $_REQUEST['taxregion'] ) ) {
		//update the session var
		$_SESSION['taxregion'] = $_REQUEST['taxregion'];

		//not empty? setup the tax function
		if ( ! empty( $_REQUEST['taxregion'] ) ) {
			add_filter('pmpro_tax', 'agst_pmpro_tax', 10, 3);
		}
	} elseif ( !empty( $_SESSION['taxregion'] ) ) {
		//add the filter
		add_filter( 'pmpro_tax', 'agst_pmpro_tax', 10, 3 );
	} else {
		//check state and country
		if ( !empty( $_REQUEST['bcountry'] ) ) {
			$bcountry = trim( strtolower( $_REQUEST['bcountry'] ) );
			if ( $bcountry == 'au' ) {
				//billing address is in AU
				add_filter( 'pmpro_tax', 'agst_pmpro_tax', 10, 3 );
			}
		}
	}
}
add_action( 'init', 'agst_pmpro_region_tax_check' );
 
//remove the taxregion session var on checkout
function agst_pmpro_after_checkout() {
	if ( isset( $_SESSION['taxregion'] ) ) {
		unset( $_SESSION['taxregion'] );
	}
}
add_action( 'pmpro_after_checkout', 'agst_pmpro_after_checkout' );

Restrict comments to members

<?php
/**
 * Require a membership level to view comments on any post.
 *
 * title: Require a membership level to view comments on any post.
 * layout: snippet
 * collection: restricting-content
 * category: content, restriction, admin-access, comments
 * link: https://www.paidmembershipspro.com/restricting-comments-on-public-posts-to-members-only/
 *
 * You can add this recipe to your site by creating a custom plugin
 * or using the Code Snippets plugin available for free in the WordPress repository.
 * Read this companion article for step-by-step directions on either method.
 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
 */
/**
 * Makes all comments on posts, including open/public posts to require a membership level to view these.
 * PMPro by default restricts comments in restricted posts to the relevant membership level. 
 */
function my_pmpro_hide_comments_on_open_posts( $comments ) {
	// Restrict comments to only logged-in members that have any active membership level.
	if ( function_exists( 'pmpro_hasMembershipLevel' ) && ! pmpro_hasMembershipLevel() ) {
		$comments = array();
		add_action( 'comment_form_before', 'pmpro_non_member_text_before_comment_form' );
	} 

	return $comments;
	
}
add_filter( 'comments_array', 'my_pmpro_hide_comments_on_open_posts' );

/**
 * Helper function to display the message that the comments are for members only.
 * Programatically loads the message from the above function.
 * 
 * Filters the comments wording for non-members to be more appropriate.
 *
 * @return string $pmpro_comments_non_member_text The message to display to non-members when viewing the comment section.
 */
function pmpro_non_member_text_before_comment_form() {
	$pmpro_comments_non_member_text = pmpro_get_no_access_message( null, null, null );
	$pmpro_comments_non_member_text = str_replace( __( 'This content is', 'pmpro-comments' ), __( 'Comments are', 'pmpro-comments' ), $pmpro_comments_non_member_text );

	echo wp_kses_post( $pmpro_comments_non_member_text );
}

Default Country

<?php
/**
 * Use the pmpro_default_country filter to pre-set the dropdown at checkout to your country of choice.
 * *
 * title: Change the Default Country of Your Membership Site Checkout.
 * layout: snippet
 * collection: misc
 * category: checkout
 * link: https://www.paidmembershipspro.com/change-the-default-country-of-your-membership-website/
 *
 * You can add this recipe to your site by creating a custom plugin
 * or using the Code Snippets plugin available for free in the WordPress repository.
 * Read this companion article for step-by-step directions on either method.
 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
 */
function my_set_pmpro_default_country( $default_country ) {
	// Set country code to "GB" for United Kingdom.
	$default_country = 'GB';
	return $default_country;
}
add_filter( 'pmpro_default_country', 'my_set_pmpro_default_country' );

Stripe % fee

<?php
/**
 * Modify the application fee percentage that is charged on Stripe transactions.
 *
 * title: Adjust Fee to Stranger Studios When Using Stripe Connect
 * layout: snippet
 * collection: payment-gateways, stripe
 * category: fee
 *
 * You can add this recipe to your site by creating a custom plugin
 * or using the Code Snippets plugin available for free in the WordPress repository.
 * Read this companion article for step-by-step directions on either method.
 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
 */

function my_pmpro_set_application_fee_percentage( $application_fee_percentage ) {
	return 0; // Remove the application fee.
	// return 2.5; // Increase application fee to 2.5%
}
 add_filter( 'pmpro_set_application_fee_percentage', 'my_pmpro_set_application_fee_percentage' );

Unlock post on a specific date

<?php
/**
 * Filter to only allow access to a protected post by post ID after a specific calendar date.
 * Learn more at: https://www.paidmembershipspro.com/unlock-content-specific-dates/
 *
 * Update the $posts_in_series_with_dates array with your post IDs => date (formatted as YYYY-MM-DD).
 *
 * title: Unlock post IDs on specific dates.
 * layout: snippet
 * collection: restricting-content
 * category: content, restriction
 *
 * You can add this recipe to your site by creating a custom plugin
 * or using the Code Snippets plugin available for free in the WordPress repository.
 * Read this companion article for step-by-step directions on either method.
 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
 */
function my_pmpro_has_membership_access_on_date( $hasaccess, $post, $user ) {
	// If they already don't have access, return.
	if ( ! $hasaccess ) {
		return $hasaccess;
	}

	// Set the post IDs and dates in an array.
	$posts_in_series_with_dates = array(
		'123' => '2023-10-28',
		'456' => '2023-10-29',
		'789' => '2023-10-30'
	);

	// Check if the post is in the array.
	if ( array_key_exists( $post->ID, $posts_in_series_with_dates ) ) {
		// Check if the current time of the WordPress site server is less than the date in the array.
		if ( current_time( 'timestamp' ) < strtotime( $posts_in_series_with_dates[$post->ID], current_time( 'timestamp' ) ) ) {
			$hasaccess = false;
		}
	}

	return $hasaccess;
}
add_filter( 'pmpro_has_membership_access_filter', 'my_pmpro_has_membership_access_on_date', 10, 3 );

Redirect Non Members to another URL

<?php
/**
 * Redirect non-members away from specific URLs.
 * Becareful, the code will redirect any URL *containing* 
 * the strings in the $urls array. E.g., if /not-locked/members/
 * is public and you have /members/ in the list, it will still
 * redirect non-members away from that URL.
 *
 * title: Redirect non-members away from specific URLs.
 * layout: snippet
 * collection: restricting-content
 * category: content, restriction, redirects
 *
 * You can add this recipe to your site by creating a custom plugin
 * or using the Code Snippets plugin available for free in the WordPress repository.
 * Read this companion article for step-by-step directions on either method.
 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
 */
function my_redirect_nonmembers() {
	// Make sure PMPro is active.
	if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
		return;
	}
	
	// Ignore members. Change to check for specific levels.
	if ( pmpro_hasMembershipLevel() ) {
		return;
	}
	
	// Ignore admins.
	if ( current_user_can( 'manage_options' ) ) {
		return;
	}
	
	// Update this array.
	$not_allowed = array(
		"/members/",
		"/groups/",
		"/groups/create/"
	);
	
	// Get the current URI.
	$uri = $_SERVER['REQUEST_URI'];
		
	// If we're on one of those URLs, redirect away.
	foreach( $not_allowed as $check ) {
		if( strpos( strtolower( $uri ), strtolower( $check ) ) !== false ) {
			// Go to levels page. Change if wanted.
			wp_redirect( pmpro_url( 'levels' ) );
			exit;
		}
	}
}
add_action( 'template_redirect', 'my_redirect_nonmembers' );

Pause Membership Access

<?php
/**
 * Mark a member "paused" and deny access to members-only content.
 * Show a message that their account is paused containing a link to your website contact page.
 *
 * title: Mark a member "paused" and deny access to members-only content
 * layout: snippet
 * collection: restricting-content
 * category: content, restriction, member-access
 *
 * link:  https://www.paidmembershipspro.com/block-pause-members-access-restricted-content/
 *
 * You can add this recipe to your site by creating a custom plugin
 * or using the Code Snippets plugin available for free in the WordPress repository.
 * Read this companion article for step-by-step directions on either method.
 * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
 */
function my_pmprouf_init_pause_access_new() {
	if ( ! function_exists( 'pmpro_add_user_field' ) ) {
		return false;
	}

	// Bail if not in administrative area.
	if ( ! is_admin() ) {
		return;
	}

	pmpro_add_field_group( 'Access to Member Content' );

	// Define the field.
	$field = new PMPro_Field(
		'pmpro_paused_user',
		'checkbox',
		array(
			'label'   => 'Pause User',
			'text'    => 'Deny Access to Member Content',
			'profile' => 'admins',
		)
	);

	pmpro_add_user_field( 'Access to Member Content', $field );
}
add_action( 'init', 'my_pmprouf_init_pause_access_new', 10 );

/**
 * Deny access to member content if user is 'paused'.
 */
function paused_member_pmpro_has_membership_access_filter( $access, $post, $user, $levels ) {
	// If no access already, or no user/levels, leave access unchanged.
	if ( ! $access || empty( $user ) || empty( $user->ID ) || empty( $levels ) ) {
		return $access;
	}

	// Check if user is paused.
	$paused_user = get_user_meta( $user->ID, 'pmpro_paused_user', true );
	if ( ! empty( $paused_user ) ) {
		$access = false;
	}

	return $access;
}
add_filter( 'pmpro_has_membership_access_filter', 'paused_member_pmpro_has_membership_access_filter', 10, 4 );

/**
 * Show a different no-access message for users with paused membership.
 */
function paused_member_pmpro_no_access_message_body( $body, $level_ids ) {
	global $current_user;

	// Check if the current user is paused.
	$paused_user = get_user_meta( $current_user->ID, 'pmpro_paused_user', true );
	if ( ! empty( $paused_user ) ) {
		$body = __( '<p>Your membership is paused. Please contact us to reinstate your membership.</p><a href="/contact/">Contact Us</a>', 'paid-memberships-pro' );
	}

	return $body;
}
add_filter( 'pmpro_no_access_message_body', 'paused_member_pmpro_no_access_message_body', 10, 2 );

/**
 * Show a message on the Membership Account page for paused members.
 */
function paused_member_pmpro_membership_account_filter( $content ) {
	global $pmpro_pages, $current_user;

	if ( is_user_logged_in() && is_page( $pmpro_pages['account'] ) ) {
		$paused_user = get_user_meta( $current_user->ID, 'pmpro_paused_user', true );
		if ( ! empty( $paused_user ) ) {
			// Message to show on the account page.
			$new_content = __( '<div class="pmpro_content_message"><p>Your membership is paused. Please contact us to reinstate your membership.</p><a href="/contact/">Contact Us</a></div>', 'paid-memberships-pro' );
			$content = $new_content . $content;
		}
	}

	return $content;
}
add_filter( 'the_content', 'paused_member_pmpro_membership_account_filter', 10 );