0

I looked at WP's site and didn't see a role I could use. I can create my own, but how do I limit the content on the page itself? Some private and some public information can be on the same page. I could create two pages, but I didn't know if WP had a way to do this or where I might build my security to allow this.

How can I restrict access to specific parts of a page, not just the page itself?

3 Answers 3

1

If you're wanting to limit by a capability (built in or custom) you can use current_user_can() and pass the appropriate capability.

if( current_user_can('manage_options') ) {
    echo "Hi there user who can manage options';
}

Keep in mind that the post or page content is one chunk and you would need to do something much more custom to hide certain paragraphs etc.

In that case you may need a shortcode to wrap the content you want to hide - actually that is a great idea for a plugin that most likely already exists :)

EDIT:

I wrote this up quickly. Needs a bit of testing. The cap is optional and will default to having to be logged in to see the content if not set.

class Hide_Content {

    function render_shortcode( $atts, $content = "" ) {
        //get the atts passed to the shortcode
        $atts = shortcode_atts( array(
            'cap' => false,
        ), $atts );

        //we need to be logged in either way
        if( is_user_logged_in() ) {

            // there is a cap set and the user can do it - we're good
            if( $atts['cap'] && current_user_can( $atts['cap']) ) {
                return "content {$atts['cap']} = $content";
            }else{

                //if there is no cap set - we'll just show it to logged in users
                return 'Logged in only :: ' . $content ;
            }
        }
    }
}
add_shortcode( 'hide_content', array( 'Hide_Content', 'render_shortcode' ) );

//useage
[hide_content cap="manage_options"]This is hidden from non-logged in and users who can't manage options[/hide_content]
1
  • I saw one that claimed it. I'd like to roll my own, maybe. It is going to need to be highly customized, and I have to maintain it. I haven't decided.
    – johnny
    Aug 7, 2014 at 16:35
0

Although this was mean to block the whole page, it can be used to block just parts of a page.

<?php if (post_password_required()):?>

<!-- Content that's not public -->

<?php endif;?>

Then you just need to password protect that page. In order for this to work properly, it would be ideal for this page to have it's own template or to have a conditional checking which page you're in first and then decide whether to block the content.

1
  • Wouldn't two pages violate DRY?
    – johnny
    Aug 7, 2014 at 16:01
0

There is is_user_logged_in() - example from codex page:

if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
}
2
  • Isn't that only for if the user is logged in?
    – johnny
    Aug 7, 2014 at 16:03
  • True, but you haven't specified this in your question. Besides you are talking about roles, which really only makes sense for logged in users. Last but not least, the answer/information isn't just for you, but for anyone coming to this. @johnny Aug 7, 2014 at 16:19

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.