0

I use below function (Nothing here is protection message):

function show_user_content($atts,$content = null){
  global $post;
    if (current_user_can('subscriber')){
    return $content;
  }
    return "Nothing here";
}
add_shortcode('RESTRICTROLE','show_user_content');

Then in post content:

[RESTRICTROLE]some word for subscriber[/RESTRICTROLE]

I need to be able to write the protection message in post content instead of in function.

My goal is to use multiple protection message by using single function.

0

Shortcode API supports arbitrary attributes, so you could easily implement it with one like:

[RESTRICTROLE message="Really nothing here"]some word for subscriber[/RESTRICTROLE]
0
0

You can use add a custom field in each of the posts that you want to have protection message to save that custom message to the post meta. Then in the shortcode you can use the get_post_meta to get that message, and you can also have a default protection message in case the post doesn't have any custom protection message.

function show_user_content($atts,$content = null){
   global $post;
   if (current_user_can('subscriber')){
        return $content;
   }
   $msg = get_post_meta($post->ID, 'protection_message', true);
   if ( !empty($msg) ){
      return $msg
   }
      return "default message";
}
add_shortcode('RESTRICTROLE','show_user_content');
0

In order to hide certain parts of your content with just a simple shortcode you may give "Restrict Anonymous Access" plugin a try:

https://wordpress.org/plugins/restrict-anonymous-access/

Examples:

[member]Some content I want to hide.[/member]

This shortcode can be placed wherever you need to hide something from logged-out users or even users of a certain user role can be addressed:

[member role="author"]Some content I want to hide from users below author role.[/member]

Also a custom note to the user can be returned:

[member role="contributor" infotext="This text will be shown to users below contributor role."]Some content I want show to subscribers or above.[/member]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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