by Ron

Permission based content

We needed a simple permission based content plugin. So, I wrote one which adds a couple shortcodes where you can specify what permission is required to view the content. Usage is as follows:

[visitor]This content will only be shown to a visitor. 
It will not be shown to any logged in user.[/visitor]
[user]This content will shown to any authenticated user.
 It will not be shown to any visitor.[/user]
[user can=read]This content will shown to any user with 
subscriber or higher privs.[/user]
[user can=edit_published_posts]This content will shown 
to any user with author or higher privs.[/user]

See the Codex for the list of permissions.

Download it here: [download#24]

#content#permissions#plugin#shortcode#wordpress

Comments

  1. Thomas

    Hi!

    Thank you for this little plugin! I tweaked it a little bit as I needed to make some shortcode working inside this plugin shortcode. For this purpose, I changed the $content to do_shortcode($content)

    This is a life saver plugin! 🙂

    • Hi – can you post your code how you changed it to allow shortcodes within shortcodes as i’ve tried a number of things to no avail – e.g., i modifed Andrea and Ron’s original to this ->

      [snipped]

      but when i put the following shortcodes in a page like this ->

      [visitor]

      To create your FREE Community Ads you must be [cs-loginlink] to do so …

      God Bless!

      [/visitor]

      [user]

      [formidable id=9]

      [/user]

      what i get is the cs-loginlink jumps out of context when page is viewed …
      so it reads and looks silly thus i know i’m missing something with shortcodes within shortcodes …

      thanks in advance for any pointers you can share … cordially, chuck scott

      ps – here is my custom login shortcode from child theme functions file ->

      /* CS - create shortcode for customized Log in with page redirect back to page before login */
      add_shortcode( 'cs-loginlink', 'cs_login_link' );
      function cs_login_link() {
      return wp_loginout( get_permalink() );
      }

      • Ron

        You can’t do shortcodes within shortcodes. The post above does not suggest that you can. You can use multiple shortcodes in a post as long as they are not nested.

        • Hi Ron – thanks for quick response … turns out i was able to get the example above to work and that my problem had to do with my shortcode cs-loginlink and the use of wp_loginout … once i changed from wp_loginout to wp_login_url then things started to come together (see new shortcode function below) …

          then when i put my login shortcode within your [visitor] shortcode, that indeed did not work but i found two conditions in which it would work …

          a) if i added the following filter to my child theme’s function file, then i could have my login shortcode work within your visitor shortcode ->

          add_filter('the_content', 'do_shortcode'); */

          b) the other alternative which was alluded to above by Thomas also seemed to work in that if i commented out the filter i just mentioned, and modified your code as follows it too allowed the login link to work within the visitor link –

          /* Add Ron and Andrea Content by Permission v 0.1 */
          function ra_user_content( $atts, $content = '' ) {
          if( empty( $atts ) && is_user_logged_in() )
          return do_shortcode($content);
          if( !empty( $atts['can'] ) && current_user_can( $atts['can'] ) )
          return do_shortcode($content);
          return '';
          }
          function ra_visitor_content( $atts, $content = '' ) {
          if( !is_user_logged_in() )
          return do_shortcode($content);
          return '';
          }
          add_shortcode( 'user', 'ra_user_content' );
          add_shortcode( 'visitor', 'ra_visitor_content' );

          accordingly, do you see any danger using the modified version of your visitor user code with said shortcode enhancements ..??.. currently it seems to work in IE and Firefox and is not generating any php errors that i can see on server …

          truly appreciate your input and certainly all the code love you and Andrea share with WP community … thanks in advance for attention to this … cordially, chuck scott

          ===========================
          My New shortcode for login link ->
          ===========================

          /* CS - create shortcode for customized Log in with page redirect back to page before login */
          add_shortcode( 'cs-loginlink', 'cs_login_link' );
          function cs_login_link($attr, $content) {
          $out = 'login';
          return do_shortcode ( $out );
          }

          • oops … the $out line seemed to get truncated even though i surrounded it with code tags so will try again but using long hand –


            original - $out = 'login';

            long hand - dollar sign out = span style font weight bold href wp_login_url get permalink title = click here .... login word close a close span end php line

  2. Richard

    Hi there – i just wanted to say thanks for this too. Really helpful.

    Richard

Leave a Reply

Your email address will not be published / Required fields are marked *