I'm working on a theme and i am wondering how i can hide specific content and functions from certain user roles via PHP. I am working with a lot of prebuilt functions so what i am after is some code snippet that i can wrap around the functions i want so it doesent display to that spesific user role. Theme will have custom user roles and custom post types. I am new to wordpress dev so this is my first theme and i thought this was the best place to ask this.


1 Answer 1


If you're new in WordPress dev, I would recommend this plugin https://wordpress.org/plugins/members/, there is an option to allow you to disable content based on User role, you can also easily creating role with the plugin.

As for your question: if you don't want the user with role subscriber for viewing content, I'm using twentysixteen theme as an example

// Start the loop.

if( have_post() ) :
    while ( have_posts() ) : the_post();

         * Include the Post-Format-specific template for the content.
         * If you want to override this in a child theme, then include a file
         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
        if( current_user_can( 'edit_posts' ) ) :
            // can accessed by contributor and the others but not subscriber
            get_template_part( 'template-parts/content', get_post_format() );


    // End the loop.

'edit_posts' role is a capability which subscriber do not have, you can view more about roles and capability chart here https://codex.wordpress.org/Roles_and_Capabilities#Capability_vs._Role_Table

  • Thank you so much! I'll read about it. Exactly what i was looking for:)
    – Dennis
    Commented Mar 22, 2016 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.