I’m looking for a way to make the plugin autodiscover custom post types (CPTs) used by your site and make sure that the PMPro meta box shows up on each page. In the mean time, You can add code like this to your active theme’s functions.php (or in a stand alone plugin) to get the meta box showing up for your CPTs:

Update: I am no longer “looking for a way to make the plugin autodiscover CPTs”. It doesn’t really make sense because we have no way of knowing what the CPT is or how to protect it for members. They are by definition custom. Adding the checkboxes to a CPT would only be the first step to locking down a CPT. For example, if an ecommerce product were marked for members only, does this mean users shouldn’t see the product at all? Or should they just not be allowed to purchase it or add it to their cart? Should the product show up in searches? etc etc

Still the code above is useful for plugin developers who want to add the checkboxes to their CPTs and are ready to take the next steps to lock down their CPTs. By default, PMPro will still filter anything output through the the_content() function. You will want to either use code like this to redirect away from member content or update your theme/etc to lock down the CPT content appropriately.

Our strategy now is to work with plugin developers to help them update their plugins to support Paid Memberships Pro or develop official addons for popular plugins with CPTs (bbPress, BuddyPress, ecommerce plugins).


This entry was posted by Jason Coleman in Recipes and tagged . Bookmark the permalink. Last updated: July 23, 2012. Titled Add PMPro Membership Level Meta Box to a Custom Post Type

Comments (12)

Hi Jason, very nice interface and I like what I see so far.

To possibly save you some time, here is a function I wrote and use to grab information about custom post types in wordpress:

# Grabs post types using array of arguments
# Returns array of post types matching arguments passed in
# Keys are the post type names
# Values can obviously be tweaked, but I just grab what is shown.
# Features are to show capabilities so I can check for them as needed.
# Notice that I only grab public post types with this.
function getPostTypesData($aArgs = array()) {
$aCust = array();
$isOK = true;

$aCurrent = get_post_types($aArgs, ‘object’);
if ( count($aCurrent) > 0 ) {
foreach ( $aCurrent as $sTypeName => $oData ) {
if ( $oData->public ) {
if ( !isset($_wp_post_type_features) ) {
global $_wp_post_type_features;
}

if ( isset($_wp_post_type_features[“{$sTypeName}”]) ) {
$aFeatures = $_wp_post_type_features[“{$sTypeName}”];
} else {
$aFeatures = array();
}
$aCust[“{$sTypeName}”] = array(
‘dispName’ => “{$oData->label}”,
‘hierarchical’ => $oData->hierarchical,
‘public’ => $oData->public,
‘ui’ => $oData->show_ui,
‘features’ => $aFeatures,
‘reqcap’ => $oData->cap->edit_post
);
} else {
$aCust[“{$sTypeName}”] = false;
}
}
}
return $aCust;
}
# Grab all custom post types
function getCustomPostTypes() {
$aArgs = array(‘_builtin’ => false);
return $this->getPostTypesData($aArgs);
}
# Grab all builtin post types
function getBuiltinPostTypes() {
$aArgs = array(‘_builtin’ => true);
return $this->getPostTypesData($aArgs);
}

Anywise, hopefully it doesn’t show up in a garbled manner. I use this in objects that I want to be aware of post types. Might save you some digging around.

Thanks for the great plugin.

Privateer, I’ll approve this cause other might use it. I’ve thought about doing stuff like this to automatically detect custom post types and add the meta box, however it kind of goes against our philosophy with PMPro. Let me explain.

The main problem is that if we enable that box for a CPT, we don’t have any way of knowing what it means for that CPT to be members only. E.g., what does it mean if a “product” CPT is members only? That it’s hidden to non-members? That non-members can’t buy it? Out of the box, PMPro may (may) hide the description field from non-members. It wouldn’t work how people want.

So we think that if you are using a CPT it’s either (a) a popular plugin which will have an Add On from us eventually or perhaps native support for PMPro built into the plugin or (b) a bit of custom CPT code, in which case you are comfortable adding the code to add the meta box.

I hope this clears up why we haven’t implemented this fairly simple feature yet. Thanks.

That’s kind of our point here that we can’t anticipate how CPT’s are being used on your site and how to lock them down. You’ll need to custom code something. This might be a good starting point: Redirect Non-members Away from Member Content

Great Jason, thanks, this worked perfectly, just changed where you have ‘product’ to the name of my custom post type, and done. Mucho gracias!

Hi, could you advise what parts of the code pasted above by Jason I would need to amend with my custom post type name? I.e. do I simply replace the work ‘product’ with the CPT?

Hello, tried this code and everything works fine, my question is, how to chang the design of the info, for closed content. Another problem is, that visitors can´t read a excerpt of the content. Only the info for closed content is shown.

Yes I know, but it´s not really working on custom types. There visitors can´t read a excerpt of the content, it´s not showing.

Leave a Reply

Your email address will not be published. Required fields are marked *