My previous post, “Allow WordPress Users to Upload Images” discussed the use of the functions.php file to implement capabilities to the contributors’ role that isn’t there by original design. As the functions.php file is part of a WordPress theme, and if an alternate theme is selected, the functions will no longer be accessible unless the functions.php file is edited in that theme as well. With the use of plugins, however, the functions will remain.
This function is slightly different than many others, in that it is a persistent change. So even if the function is not enabled as a plugin or removed from a functions.php the change will remain until it is explicitly revoked. Two plugins are needed.
This plugin enables the capability of the contributor role to upload content along with their post. Once enabled the action takes effect, even if it is then disabled. Hence, the reason for the next plugin.
<?php /* Plugin Name: Armedia: Contributor Role Upload Enabler Description: Adds the capability to the contributor role to upload content. This change is persistent until it is explicitly revoked. Based on the source by Hardeep Asrani. Author: Paul Combs Version: 1.0 Author URI: https://www.armedia.com */ function allow_contributor_uploads() { if ( current_user_can( 'contributor' ) && ! current_user_can( 'upload_files' )) { $contributor = get_role('contributor'); $contributor->add_cap('upload_files'); } } add_action('admin_init', 'allow_contributor_uploads'); ?>
This plugin removes the capability of the contributor role to upload content. Once enabled the action takes effect, even if it is then disabled.
<?php /* Plugin Name: Armedia: Contributor Role Upload Disabler Description: Removes the capability to the contributor role to upload content. This change is persistent until it is explicitly revoked. Author: Paul Combs Version: 1.0 Author URI: https://www.armedia.com */ function remove_contributor_uploads() { if ( current_user_can( 'contributor' ) && current_user_can( 'upload_files' )) { $contributor = get_role('contributor'); $contributor->remove_cap('upload_files'); } } add_action('admin_init', 'remove_contributor_uploads'); ?>
There are no checks and balances here, so it should be noted that if both are enabled the results will not be as expected. A quick test of refreshing a contributor screen with both plugins enabled will reveal that the capability is available every other refresh. For the expected result, select one or the other.
0 Comments