0

anyway to block any user /role(except the admin) to use shortcode? i want to keep some specific shortcode secret. if somehow users come to know, they cant use it. if only admin writes the shortcode it will make changes to site, but if someone else write the shortcode it will be taken as a simple text.

2
  • I think you need to check the rendered post author, and if the author is not an admin, prevent those shortcodes from being rendered. Nov 26, 2020 at 7:58
  • thaMks brother. Nov 28, 2020 at 9:30

1 Answer 1

0

i think this might work with you

function myshortcode(){

$user = wp_get_current_user();
if ( !in_array( 'author', (array) $user->roles ) ) {
    //Run shortcode
}

}

add_shortcode('myshortcode','myshortcode');
3
  • 1
    That depends on the user who's viewing the content though, not the user who added the shortcode to the page?
    – Rup
    Nov 26, 2020 at 11:07
  • it's limit shortcode usage based on user role Nov 26, 2020 at 12:39
  • Bruh, take my heart. Nov 28, 2020 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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