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 1: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
    $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

If there is a false accusation made against you, then remove this fake report.

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
Site Admin
Posts: 495
Joined: Sun Jan 12, 2014 11:18 pm
Location: New York City

Return to WordPress

Who is online

Users browsing this forum: No registered users and 0 guests