WordPress and buddypress programming

Restrict Media Library Access Manually

Viewing 0 reply threads
  • Author
    Posts
    • #1740
      michael mc
      Keymaster
      @michael

      #wordpress #buddypress #code
      if you want to add a custom user role or simply don’t want to use a plugin, then you can try this method instead. It uses the same code used by the plugin, but you will be able to modify it to meet your needs.

      This method requires you to add code to your WordPress files. If you haven’t done this before, then take a look at our guide on how to copy and paste code in WordPress.

      You’ll need to add the following code to your WordPress functions.php file or a site-specific plugin.

      // Limit media library access
        
      add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments' );
       
      function wpb_show_current_user_attachments( $query ) {
          $user_id = get_current_user_id();
          if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts
      ') ) {
              $query['author'] = $user_id;
          }
          return $query;
      } 

      This code uses current_user_can function to check if the user has the capability to activate plugins or edit other user’s posts. If they don’t, then it changes the query used to display media files and limit it to user’s ID.

Viewing 0 reply threads
  • You must be logged in to reply to this topic.