0

okay so I'm using the following code to show different content to users with different levels in WordPress

    <?php global $user_ID; if( $user_ID ) : ?>
<?php if( current_user_can('level_10') ) : ?>

<a href="http://techyoucation.com/wp-admin/">Admin</a>

<?php else : ?>

FREE

<?php endif; ?>
<?php endif; ?>

How can I get different content to show for users with level 10, level 9, level 8, level 7 ect...

Thanks in advance

1 Answers1

0

there are many ways to do level based filtering - and it depends on what you want to do exactly, but basically you just need simple conditionals ( if - else or if - then or switch statements ) again - depends on context.

if( current_user_can( 'level_10' ) ){
 echo  'A - content for user level 10';
} elseif( current_user_can( 'level_8' ) ) {
 echo  'B - content for user level 8 and above';
} elseif( current_user_can( 'level_6' ) ) {
 echo  'C - content for user level 6 and above';
} // ok for wordpress < 3.0

/*
* Please read my note below regarding user levels to roles conversion
*/
if( current_user_can( 'manage_options' ) ){
 echo  'A - content for user level Admin';
} elseif( current_user_can( 'publish_pages' ) ) {
 echo  'B - content for user level Editor and above';
} elseif( current_user_can( 'publish_posts' ) ) {
 echo  'C - content for user level Author and above';
} // ok for wordpress > 3.0

which will output totally different content for each user , but also means that user level 10 will NOT SEE the content of level 6.. ( unless you drop the else.. )

// when Admin logged
A - content for user level Admin
// when level editor and above logged
B - content for user level Editor and above
// when level author and above logged
C - content for user level Author and above

or

 if( current_user_can( 'publish_posts' ) ){ // Author
    echo  'A - content for Author and above';

      if( current_user_can( 'publish_pages' ) ){ // Editor 
    echo  'B - Additional content for Editor and above';

           if( current_user_can( 'manage_options' ) ){ // Administrator
              echo  'C - Additional content for administrator ';

          }

        }

    }

which will ADD output based on user levels - so user 10 see user 6 content PLUS user 8 content PLUS user 10 content

Put in plain human language example one will show content for level 10 OR content for level 8 OR .. while example 2 will show content for level 10 AND content for level 8 AND ..

Like said before - there are many many ways to use it , but it all depends on context .

Note : Since wp 3.0 the user_level system was deprecated . you will need to filter by Capabilities using the user level to role Conversion system ..

if( current_user_can( 'administrator' ) ){} // only if administrator
if( current_user_can( 'editor' ) ){} // only if editor
if( current_user_can( 'author' ) ){} // only if author
if( current_user_can( 'contributor' ) ){} // only if contributor
if( current_user_can( 'subscriber' ) ){} // only if subscriber
Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • Thanks for the reply but I need some extra help. I want the code for showing different text for different user levels E.G Level 10 will see the work admin with link to admin dashboard for easy access. Level 9 will see the text PRO level 8 will see Advance and level 7 will see FREE... So I think I need your first code, but can't get it to work. Is there any change you can edit it to include the `` tags so I can just copy and paste it and only need to edit the content and levels. Thanks So much – Techyoucation Nov 17 '13 at 11:19
  • @Techyoucation - I am sorry . Since you asked about `user levels` specifically ,I assumed you know what you are asking and that you might use and older version ( yep - some still do ). This system is deprecated now . please read my update and Note . – Obmerk Kronen Nov 18 '13 at 04:11
  • Hello, No I'm using the latest version of WP but use a plugin called "restrict content pro" which uses levels... The code first code you supplied does not work, but I could be using it wring as I have no PHP knowledge and found the code online. - Thanks - Aled – Techyoucation Nov 18 '13 at 08:38
  • I place the code you sypplid but it only words for level 10, any other level nothing shows? – Techyoucation Nov 19 '13 at 08:49