Title: Limiting Shortcode
Last modified: December 10, 2020

---

# Limiting Shortcode

 *  Resolved [muza11](https://wordpress.org/support/users/muza11/)
 * (@muza11)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/)
 * i want to put this code into shortcode.php so that only if admin write down the
   shortcode it will work and any people can view that. but if another user write
   down it will be taken as a simple text not as a shortcode, i mean it wont work.
   i am kinda a confused where i should put my code. here is a example code. you
   can modify.
 *     ```
       function myshortcode(){
   
       $user = wp_get_current_user();
       if ( !in_array( 'author', (array) $user->roles ) ) {
           //Run shortcode
       }
   
       }
   
       add_shortcode('myshortcode','myshortcode');
       ```
   

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Plugin Author [Deepak Ghimire](https://wordpress.org/support/users/deip/)
 * (@deip)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13782052)
 * Hi,
 * We suggest not doing it with any code files inside the plugin.
 * If the author is not an admin. Unregister the shortcode and the shortcode won’t
   be executed.
    [https://developer.wordpress.org/reference/functions/remove_shortcode/](https://developer.wordpress.org/reference/functions/remove_shortcode/)
 * Our shortcode tag is `dflip`
    Just remove that shortcode using code in custom
   theme’s function.php
 * Best,
    Deip
 *  Thread Starter [muza11](https://wordpress.org/support/users/muza11/)
 * (@muza11)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13782540)
 * i am not much familiar with coding. will this work? unregistering shortcode….
   kinda seems scary. will admin still able to use shortcode? will this code unregister
   only for author? again if only unregister for author, can he still view the result
   when admin input shortcode? i am so confused.
 *     ```
       function remove_shortcode( $tag ) {
           $user = wp_get_current_user();
           if ( !in_array( 'author', (array) $user->roles ) ) {
               global $shortcode_tags;
   
               unset( $shortcode_tags[ $tag ] );
           }
       }
       ```
   
 *  Plugin Author [Deepak Ghimire](https://wordpress.org/support/users/deip/)
 * (@deip)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13784842)
 * Hi,
 * The function is already built by WordPress, you don’t need to redefine it.
 * Please look around for code examples.
    [https://developer.wordpress.org/reference/functions/remove_shortcode/](https://developer.wordpress.org/reference/functions/remove_shortcode/)
 *     ```
       add_action( 'init', 'remove_my_shortcodes',20 );
       function remove_my_shortcodes() {
           remove_shortcode( 'myShortcode' );
       }
       ```
   
 * Please make amendments to that function as per your need.
 * [https://wordpress.org/support/topic/removing-shortcode-usage-for-specific-user-role/](https://wordpress.org/support/topic/removing-shortcode-usage-for-specific-user-role/)
 * [https://stackoverflow.com/questions/40960756/how-to-disable-shortcodes-in-pages-posts-with-a-certain-string-in-the-url](https://stackoverflow.com/questions/40960756/how-to-disable-shortcodes-in-pages-posts-with-a-certain-string-in-the-url)
 * Best,
    Deip
 *  Thread Starter [muza11](https://wordpress.org/support/users/muza11/)
 * (@muza11)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13785796)
 * but if i remove the shortcode how can i insert books. the link of forum post 
   you provided is closed and not resolved.
 *  Plugin Author [Deepak Ghimire](https://wordpress.org/support/users/deip/)
 * (@deip)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13786924)
 * That doesn’t remove the shortcode from the site. It removes the assignment of
   the function that creates flipbook and the shortcode will just be displayed as
   text instead.
 * So it affects only the content that is loaded, not the whole site, nor the plugin.
 * Just like you had asked:
 * > i want to put this code into shortcode.php so that only if admin write down
   > the shortcode it will work and any people can view that. but if another user
   > write down it will be taken as a simple text not as a shortcode, i mean it 
   > wont work.
 *  Thread Starter [muza11](https://wordpress.org/support/users/muza11/)
 * (@muza11)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13790697)
 * i did this
 *     ```
       $user = wp_get_current_user();
           if ( !in_array( 'administrator', (array) $user->roles ) ) {
               add_action( 'init', 'remove_my_shortcodes',20 );
       		function remove_my_shortcodes() {
           		remove_shortcode( 'dflip' );
       		}
           } else{
       		do_shortcode('dflip');
             }
       ```
   
 * now wp_current_user() gets the info about viewer. i dont care about who views.
   i want who writes the shortcode. any way to get something like wp_get_post_author()?
 *  Thread Starter [muza11](https://wordpress.org/support/users/muza11/)
 * (@muza11)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13791665)
 * i used this but did not worked for me. it removed shortcode for all.
 *     ```
       function remove_my_shortcodes() {
           $post_id = get_queried_object_id();
           $author_ID = get_post_field( 'post_author', $post_id );
           $authorData = get_userdata( $author_ID );
           if ( !in_array( 'administrator', $authorData->roles)){
               remove_shortcode( 'dflip' )
           }else{
               do_shortcode('dflip');
           }
       }
       add_action( 'init', 'remove_my_shortcodes',20 )
       ```
   
 *  Plugin Author [Deepak Ghimire](https://wordpress.org/support/users/deip/)
 * (@deip)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13800555)
 * Hi,
 * Try this:
 *     ```
       add_filter( 'the_content', 'filter_the_shortcode_in_the_main_loop', 1 );
   
       function filter_the_shortcode_in_the_main_loop( $content ) {
   
           global $post;
           $author_id = $post->post_author;
           $is_admin = user_can( $author_id, 'edit_users' );
   
           if ( !$is_admin) {
               remove_shortcode('dflip');
           }
   
           return $content;
       }
       ```
   
 * You can change the user, capabilities based on your need.
    If you want editors
   and authors to also have shortcode access, use ‘edit_posts’ or select related
   capabilities
 * > [Roles and Capabilities](https://wordpress.org/support/article/roles-and-capabilities/)
 * We tried this by creating a child theme and adding the code in the functions.
   php so that it stays independent of thems and plugin.
 * Best,
    Deip
 *  Thread Starter [muza11](https://wordpress.org/support/users/muza11/)
 * (@muza11)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13807846)
 * you don’t know what you did. you you just awesome. i can’t explain how much i
   owe to you. take my love full of my heart. god bless you dude. it worked.
 *  Plugin Author [Deepak Ghimire](https://wordpress.org/support/users/deip/)
 * (@deip)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13808112)
 * Hi [@muza11](https://wordpress.org/support/users/muza11/),
    Glad to know, it 
   was helpful to you.
 * Have a good day 🙂
    Deip
 * P.S. If you find our support helpful and when you have free time, feel free to
   leave a review about our plugin [https://wordpress.org/support/plugin/3d-flipbook-dflip-lite/reviews/](https://wordpress.org/support/plugin/3d-flipbook-dflip-lite/reviews/)
   
   It would mean a lot to us 🙂

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Limiting Shortcode’ is closed to new replies.

 * ![](https://ps.w.org/3d-flipbook-dflip-lite/assets/icon-128x128.gif?rev=3175518)
 * [DearFlip - PDF Flipbook, 3D Flipbook, PDF embed, PDF viewer](https://wordpress.org/plugins/3d-flipbook-dflip-lite/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/3d-flipbook-dflip-lite/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/3d-flipbook-dflip-lite/)
 * [Active Topics](https://wordpress.org/support/plugin/3d-flipbook-dflip-lite/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/3d-flipbook-dflip-lite/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/3d-flipbook-dflip-lite/reviews/)

## Tags

 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [user restriction](https://wordpress.org/support/topic-tag/user-restriction/)

 * 10 replies
 * 2 participants
 * Last reply from: [Deepak Ghimire](https://wordpress.org/support/users/deip/)
 * Last activity: [5 years, 5 months ago](https://wordpress.org/support/topic/limiting-shortcode/#post-13808112)
 * Status: resolved