How to add the Membership Access metabox to custom post types

Hello,

I've installed Protect Content and I'm interested into the post by post protection (including some of my custom post types posts).

That's why I activated the "Post by Post Protection" as well as the "Custom Post Type Protection - Post by Post".

But I've encountered 2 issues.

1. I've some notices on the "Protected Content" page, in the "Custom Post Types" tab. View screenshot attached.

2. I don't have the "Membership Access" metabox in my custom post types posts edition page. Only in the regular post edition page.

  • Ash

    Hello @Romain

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    These are notices, not error. These do not break the application but it is helpful for the developer. I am letting the developer know to fix the notices.

    Meantime, would you please disable debug mode and check if any error is shown in that page?

    To disable debug mode please edit a line in your wp-config.php:
    Change

    define('WP_DEBUG', true);

    to

    define('WP_DEBUG', false);

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Michael Bissett

    Hey @Romain, thanks for your patience here! :slight_smile:

    I can't speak for my colleague @Ashok, but I can say I'm having a pretty good night presently. :slight_smile:

    In regards to your second question, you can try enabling the "Custom Post Type Protection - Post by Post" add-on inside of Protect Content -> Add-ons.

    If the metabox still doesn't show up, then keep that add-on enabled, and insert this as an mu-plugin on your site (more on how to create one of those here):

    add_filter( 'ms_controller_membership_metabox_add_meta_boxes_post_types', 'filter_my_custom_post_type');
    
    function filter_my_custom_post_type( $post_types ) {
    
        $post_types[] = 'enter-post-slug-here';
    
        return $post_types;
    }

    Just change "enter-post-slug-here" to the slug that your custom post type uses, and you should be all set! :slight_smile:

    Hope this helps!

    Kind Regards,
    Michael

  • Ash

    Hello @Romain

    I hope you are well today.

    Would you please try to set some priority:

    add_filter( 'ms_controller_membership_metabox_add_meta_boxes_post_types', 'filter_my_custom_post_type', 10, 1);
    
    function filter_my_custom_post_type( $post_types ) {
    
        $post_types[] = 'enter-post-slug-here';
    
        return $post_types;
    }

    Or even:

    add_filter( 'ms_controller_membership_metabox_add_meta_boxes_post_types', 'filter_my_custom_post_type', 99, 1);

    Let us know if it works.

    Cheers
    Ash

  • Michael Bissett

    Hey @Romain,

    Doing well over here, good to hear you're doing well too. :slight_smile:

    Just to try one more thing here, since you mentioned your CPT possibly loading later than that filter is applied, could you try replacing the add_filter line my colleague Ashok suggested with this:

    add_filter( 'ms_controller_membership_metabox_add_meta_boxes_post_types', 'filter_my_custom_post_type', 999, 1);

    If that doesn't work, then could you please send in the following, so that I can have a closer look at what's going on here?

    - Mark to my attention, the subject line should contain only: ATTN: Michael Bissett
    - Do not include anything else in the subject line, doing so may delay our response due to how email filtering works.
    - Link back to this thread
    - Include WordPress admin access details (login address, username & password)
    - Include FTP log-in details (hostname, username & password)
    - Include the name of the post type in question
    - Include any relevant URLs for your site

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    https://premium.wpmudev.org/contact/

    Thanks a bunch! :slight_smile:

    Kind Regards,
    Michael

  • Michael Bissett

    Hey @Romain, thanks a bunch for your patience here! :slight_smile:

    After having a look inside your site, and trying various things with the file you had inserted the code into:

    /themes/regional-it/inc/protect-content.php

    I was able to get this working for you by removing the earlier code (after making a backup, of course), and instead inserting the following into your site as an mu-plugin:

    add_filter( 'ms_controller_membership_metabox_add_meta_boxes_post_types', 'rit_protect_cpt', 10, 1 );
    
    function rit_protect_cpt( $post_types ) {
    	$post_types[] = 'detached';
    	$post_types[] = 'event';
    	$post_types[] = 'interview';
    	$post_types[] = 'job';
    	$post_types[] = 'lorgnette';
    	$post_types[] = 'portrait';
    	$post_types[] = 'practice';
    	$post_types[] = 'special';
    	$post_types[] = 'tribune';
    
    	return array_unique( $post_types );
    }

    (it's called reaklab-protected-content-cpt-patch.php)

    Could you confirm that it's working as it should on your end as well please? :slight_smile:

    Kind Regards,
    Michael

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.