Disable the theme and plugin file editor
Remove the Theme File Editor and Plugin File Editor from WordPress for every user, so nobody can edit PHP files from the dashboard.
// Remove the theme and plugin file editors for every user, administrators included.
add_filter(
'map_meta_cap',
function ( $caps, $cap ) {
if ( in_array( $cap, array( 'edit_themes', 'edit_plugins', 'edit_files' ), true ) ) {
$caps[] = 'do_not_allow';
}
return $caps;
},
10,
2
);
What it does
The Theme File Editor and Plugin File Editor let administrators change PHP files from the dashboard. One typo can take the site down, and if an admin account is stolen, the editor makes it easy to plant malicious code.
This snippet takes the edit_themes, edit_plugins and edit_files capabilities away from every user, so the editor screens and their menu links disappear.
Good to know
- The strongest option is
define( 'DISALLOW_FILE_EDIT', true );in wp-config.php. Use this snippet when you can't edit wp-config.php. It works the same way and won't clash if that constant is set too. - Installing and updating plugins and themes still works.