AVeryNiceWeb

How to add a shortcode with a user role condition

How to add a shortcode with a user role condition

Postby admin » Sat Aug 05, 2017 5:34 pm

Sometimes one needs to have some content created via shortcode and to be displayed only in a specific theme php page (e.g. sample-page.php) and to a certain user role.

Here is the code for that (change roles according to your own user roles):


Code: Select all
    <?php
    $result = add_role( 'Client', __('Client' ),
    array( ) );
   
    if( current_user_can('subscriber') ) {

     if ( is_user_logged_in() ) {
        echo do_shortcode('[my-shortcode]');
    }
    } else {
        echo ''; }
     
    ?>


The code above assumes you have a 'Client" role in your WP site.
The AVNW Support Team
http://www.AVeryNiceWeb.com

We also have great domain names for your next website. Go through the list and make an offer now!

Shopping for a new website? See our design deals.

How much is your website worth? Get a free estimate.

And, if our post has provided you with great information, there are a few ways you can show your appreciation:
  1. Like us on Facebook
  2. Post a review.- Posting a good review about this site could help us provide improved support.

Thank You!
User avatar
admin
Site Admin
 
Posts: 459
Joined: Mon Jan 13, 2014 3:18 am
Location: New York City

Return to WordPress

Who is online

Users browsing this forum: No registered users and 0 guests

cron