• Resolved ricgoldman

    (@ricgoldman)


    I’ve created a custom user role called “event-author” to work with another plugin “My Calendar”. Is there any way to modify your plugin to limit that user-role as well (or can I configure my user role to be compatible with your plug-in’s restriction)?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Alvaro Gómez

    (@mrfoxtalbot)

    Hi @ricgoldman, that’s a great question!

    At the moment, the plugin only checks for roles (admin, editor, author & contributor), not for specific capabilities. This means that any custom roles will be considered as authors in terms of permissions and will only be able to see their own uploads.

    I was actually planning to improve this in the next update. I have not tested it in depth but for now, you should be able to make the plugin work with custom roles (as “event-author”) by using this code:

    /**
     * Plugin Name: Restrict Media Library Access
     * Description: Restricts access for Authors, Contributors, and any role that cannot edit other users' posts so they can only see their own Media Library uploads.
     * Plugin URI: https://www.ads-software.com/plugins/restrict-media-library-access
     * Version: 1.4.1
     * Author: mrfoxtalbot
     * Author URI: https://mrfoxtalbot.com
     * Text Domain: restrict-media-library-access
     * License: GPLv2
     */
    
    // If this file is called directly, abort.
    if ( ! defined( 'WPINC' ) ) {
        die;
    }
    // Filter Ajax view
    add_filter( 'ajax_query_attachments_args', 'mrfx_show_current_user_attachments' );
    
    function mrfx_show_current_user_attachments( $query ) {
        $user_id = get_current_user_id();
        if ( $user_id && !current_user_can('edit_others_posts')) {
            $query['author'] = $user_id;
        }
        return $query;
    }
    // Filter list view
    add_filter( 'request', 'mrfx_show_current_user_attachments_list' );
    function mrfx_show_current_user_attachments_list( $query ) {
        $screen = null;
        if ( function_exists( 'get_current_screen' ) ) { 
         $screen = get_current_screen(); 
        } 
        if ( !is_null($screen) && in_array($screen->id, array('upload') ) ) {
            $user_id = get_current_user_id();
            if ( $user_id && !current_user_can('edit_others_posts')) {
                $query['author'] = $user_id;
            }
        }
        return $query;
    } 

    With this code, any role that has the capability to edit other users’ post (edit_others_posts) will be able to see everyone’s uploads. Custom roles that can only edit their own pots, will only see their uploads.

    With any luck, I will update the plugin soon to include this improvement so you do not have to be using this custom code too long.

    Let me know if that works!

    Plugin Author Alvaro Gómez

    (@mrfoxtalbot)

    I am going to mark this as resolved, @ricgoldman but feel free to open a new thread if the above did not work. Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Restricting a custom user type.’ is closed to new replies.