mrfoxtalbot / rmla Goto Github PK
View Code? Open in Web Editor NEWRestrict Media Library Access WordPress Plugin
License: GNU General Public License v3.0
Restrict Media Library Access WordPress Plugin
License: GNU General Public License v3.0
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.
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:
/**
* 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://wordpress.org/plugins/restrict-media-library-access
* Version: 1.4.1
* Author: mrfoxtalbot
* Author URI: http://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;
}
The new Media Inserter (WordPress/gutenberg#44496) does not support filtering media by capabilities or roles yet. This means that the RMLA plugin will not be able to limit access to other user's files on sites where the block editor is active. I have flagged this upstream in WordPress/gutenberg#58944 but it needs to be fixed in Gutenberg core.
This was flagged in a forum thread here https://wordpress.org/support/topic/conflict-with-media-inserter-in-block-editor/
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.