• Resolved websavers

    (@websavers)


    Currently with UM, you can enable the *options* to restrict posts within any custom post type, but this requires that you also then enable the restrictions on each post within that custom post type. If a user is to add a new post to it, they need to then remember to also restrict its access accordingly.

    We’ve got a custom post type where everything in it should always have restricted visibility to one particular user role.

    Is there a clean way to configure Ultimate Member such that *any* post within a specific custom post type (including newly added ones) are automatically restricted to a specified user role?

    I’m sure we could find the specific UM post meta that locks down a post and auto-insert them into the post_meta table when a new post is created within that custom post type, but that seems far messier than something that would work globally.

    • This topic was modified 2 years, 5 months ago by websavers.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @websavers

    Unfortunately, this requires customization. You can try the following code snippet to save a UM Content Restriction on saving a custom post type:

    add_action("save_post","um_110921_save_content_restrictions", 99999 );
    function um_110921_save_content_restrictions( $post_id ){
    
        $content_restrictions = maybe_unserialize('a:9:{s:26:"_um_custom_access_settings";b:1;s:14:"_um_accessible";i:2;s:16:"_um_access_roles";a:1:{s:10:"subscriber";s:1:"1";}s:28:"_um_access_hide_from_queries";b:0;s:19:"_um_noaccess_action";i:0;s:30:"_um_restrict_by_custom_message";i:0;s:27:"_um_restrict_custom_message";s:0:"";s:19:"_um_access_redirect";i:0;s:23:"_um_access_redirect_url";s:0:"";}');
        unset( $content_restrictions['_um_access_roles'] );
        $content_restrictions['_um_access_roles']['subscriber'] = 1; 
        $content_restrictions['_um_access_roles']['um_user-role-a'] = 1;
       
        update_post_meta( $post_id, "um_content_restriction", $content_restrictions );
        
    }

    The above code will enable the UM Content Restriction with Subscriber( subscriber ) and User Role A ( um_user-role-a ) roles of a post/custom post type. You can change the role slug to match the roles on your site that you want to enable the content restriction. Also, the above code uses save_post as an action hook, if you have a custom post type, just change it to save_post_{your custom post type slug} e.g. save_post_myproducts.

    Regards,

    • This reply was modified 2 years, 5 months ago by Champ Camba.
    • This reply was modified 2 years, 5 months ago by Champ Camba.
    • This reply was modified 2 years, 5 months ago by Champ Camba.
    Thread Starter websavers

    (@websavers)

    Thank you @champsupertramp !

    I am not a huge fan of the need to add this data to postmeta for every post when it applies to every single one of them. It just seems wasteful to me, though I get why this is a good option for some.

    Instead of this method (and because I found the original devs of this site already had a custom page template for the post type) I modified the page template and put my display conditionals based on user role in there.

    • This reply was modified 2 years, 5 months ago by websavers.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Thanks for letting us know how you resolve the issue.

    Regards,

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Restrict an entire custom post type’ is closed to new replies.