In part 1, we introduced you to WordPress filters, and gave the first example. If you aren’t familiar with WordPress actions, hooks, and filters, you may want to peruse it now.
The next filters we will look at deal with permissions in EWWW Image Optimizer:
- ewww_image_optimizer_admin_permissions
- ewww_image_optimizer_bulk_permissions
- ewww_image_optimizer_superadmin_permissions
- ewww_image_optimizer_manual_permissions
The first controls who can access plugin admin functions like installing Pngout and accessing the settings page, the second controls access to the Bulk Optimize page(s) but is set to the same value as #1 by default: ‘activate_plugins’. The third one controls who can access the network admin settings, which is restricted to WP Super Admins by default, and the last controls who can use the one-click optimize and convert options in the Media Library. This last one is set to ‘edit_others_posts’ by default.
One example of why you might override these is because you think I’m crazy… A more realistic scenario though, is that you might be running a site for a client and reserve admin privileges for yourself, but you might want to allow the client (with the Editor role) to configure the EWWW I.O. settings or run a Bulk Optimize. Let’s go with the latter, and give you an example for that in the form of a plugin. Just like Part 1, you can copy this code into a .php file, drop it into your plugins folder and activate it.
<?php
/*
Plugin Name: EWWW Bulk Permissions
Version: .1
*/
add_filter( 'ewww_image_optimizer_bulk_permissions', 'ewww_modify_bulk_permissions' );
function ewww_modify_bulk_permissions( $permissions ) {
return 'edit_others_posts';
}
This one is much simpler than the example in Part 1, and here we simply return the permissions level that we wish to use for the Bulk Optimize page. You can find a full list of available permissions on the WordPress Roles and Capabilities page. If you want to use one of the other filters, simply replace ewww_image_optimizer_bulk_permissions with the appropriate action name from the list above. If you want to use a different permission/capability, just replace edit_others_posts (but make sure to keep the single quotes on either side).