• Skip to primary navigation
  • Skip to main content

RocketGeek

Home of WP-Members, The Original WordPress Membership Plugin

  • WordPress Plugins
    • WP-Members
      • FAQs
      • Quick Start
      • Documentation
      • Extensions
    • Advanced Options
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Download Protect
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Invite Codes
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • MailChimp Integration
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • PayPal Subscriptions
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Salesforce Web-to-Lead
    • Security
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Text Editor
      • Purchase the Plugin
      • Get the Pro Bundle
    • User List
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • User Tracking
      • Documentation
      • Purchase the Plugin
      • Get the Pro Bundle
    • Memberships for WooCommerce
    • WordPass
  • Blog
    • Basics
    • Tips and Tricks
    • Filters
    • Actions
    • Code Snippets
    • Shortcodes
    • Design
    • Release Announcements
  • Store
    • Cart
    • Checkout
  • Contact
  • Log In
  • Show Search
Hide Search
Home » Filters » Blocking Custom Post Types

Blocking Custom Post Types

Chad Butler · Nov 21, 2014 ·

This article is provided free. Find out how you can get full access to premium content, including how-to articles and support forums, as well as priority email support and member exclusive plugin extensions..

 

Note: As of WP-Members 3.0, you can add custom post types to the plugin’s main blocking options. See the options documentation section on Custom Post Types.

This is a new method of blocking custom post types. There are two main differences over the method described here and the original method.

First, this method is a little more scalable.  If you have multiple custom post types and want to set a blocking value for each, you only need to add to the settings array.  Second, this method allows you to override the blocking value on an individual custom post type post than you have set for the custom post type as a group.  This operates the same as regular posts and pages. It also takes advantage of the new extra settings that WP-Members version 3.0 offers.

(If you want to add a meta box for blocking/unblocking individual custom post type posts, there is an additional code snippet you can add here.)  

To use these code snippets, add to your functions.php file.

Main Settings

This code snippet will filter the main plugin settings to add settings for your Custom Post Types. I have indicated the main block of settings of one custom post type (acme_product). If you have additional CPTs, copy the block indicated, add one for each additional CPT before the return at the end.

This block of settings will set whether to block/unblock the CPT by default, whether to show excerpts before the post content, and whether to display the login and registration forms if blocked and not logged in.

add_filter( 'wpmem_settings', 'my_cpt_settings' );					 
function my_cpt_settings( $settings ) {
	
	/** Complete this block for any CPT **/

	// Set to the name of your custom post type
	$cpt_slug = "acme_product";

	// Block by default (1=block, 0=unblock)
	$settings['block'][ $cpt_slug ] = 1;
	
	// Show excerpts (1=yes, 0=no)
	$settings['show_excerpt'][ $cpt_slug ] = 0;
	
	// Show login form (1=yes, 0=no)
	$settings['show_login'][ $cpt_slug ] = 1;
	
	// Show registration form (1=yes, 0=no)
	$settings['show_reg'][ $cpt_slug ] = 1;
	
	/** End CPT Block **/
	
	// Return extra settings at the end.	
	return $settings;
}

This is all you need to do if you are blocking a CPT by default.

Override Default Block Settings

This code block is used if you are going to have some individual CPTs set differently than your default setting (i.e. unblock individual posts if the posts are blocked by default, or vice versa).

To make this ready to use for multiple CPTs, I have added a helper function. Update the values in the first function my_custom_post_types() to reflect your custom post types.  If you only have one, then you only need one array value.  If you have more, add as needed.

The array key should be the value of your custom post type and the value should be either “block” or “unblock”.

Here is the snippet:

/**
 * Identify your custom post types here.
 */ 
function my_custom_post_types() {
	$settings = array(
		'acme_product'  => 'block',
		'acme_product2' => 'unblock',
	);
	return $settings;
}

// You do not need to change anything after this.

add_filter( 'wpmem_block', 'block_custom_post_types' );
function block_custom_post_types( $block ) {

	global $post;
	$post_type = get_post_type( $post );
	$my_cpts = my_custom_post_types();

	// If this is a single post and it is a defined custom post type.
	if ( is_single() && ( array_key_exists( $post_type, $my_cpts ) ) ) {

		// Get the custom post type block definition.
		$cpt_block = ( $my_cpts[ $post_type ] == 'block' ) ? true : false;

		// Check for meta overrides.
		$meta = get_post_meta( get_the_ID(), '_wpmem_block', true );
		if( $cpt_block ) { 
			if ( isset( $meta ) && $meta == 0 ) {
				$block = false;
			}
		} else {
			if ( isset( $meta ) && $meta == 1 ) {
				$block = true;
			}
		}
	}
	// Return block value.
	return $block;
}

Note: if you add the meta box for blocking/unblocking individual custom post types, you only need to add the helper function my_custom_post_types() once.

Filters, Tips and Tricks filters, get_post_type, wpmem_block

Ready to get started?

Join Today!

© 2025 · butlerblog.com · RocketGeek is built using WordPress, WP-Members, and the Genesis Framework

  • butlerblog.com
  • WP-Members Support Subscription
  • Terms of Service
  • Privacy Policy
  • Refund Policy