Wordpress - How to allow editor to edit privacy page / settings only?
Editing the privacy policy page is restricted to manage_privacy_options
as pointed out in a comment in the WordPress core file wp-includes/capabilities.php
:
/*
* Setting the privacy policy page requires `manage_privacy_options`,
* so editing it should require that too.
*/
if ( (int) get_option( 'wp_page_for_privacy_policy' ) === $post->ID ) {
$caps = array_merge( $caps, map_meta_cap( 'manage_privacy_options', $user_id ) );
}
To allow users with the roles who can edit pages (in single and also multisite instances) to edit and delete the privacy policy page one has to overwrite the editor
and administrator
$caps
array:
add_action('map_meta_cap', 'custom_manage_privacy_options', 1, 4);
function custom_manage_privacy_options($caps, $cap, $user_id, $args)
{
if (!is_user_logged_in()) return $caps;
if ('manage_privacy_options' === $cap) {
$manage_name = is_multisite() ? 'manage_network' : 'manage_options';
$caps = array_diff($caps, [ $manage_name ]);
}
return $caps;
}
Update: Allow users with the role editor
or administrator
to edit and delete the privacy policy page (which is not possible per default in multisite instances):
add_action('map_meta_cap', 'custom_manage_privacy_options', 1, 4);
function custom_manage_privacy_options($caps, $cap, $user_id, $args)
{
if (!is_user_logged_in()) return $caps;
$user_meta = get_userdata($user_id);
if (array_intersect(['editor', 'administrator'], $user_meta->roles)) {
if ('manage_privacy_options' === $cap) {
$manage_name = is_multisite() ? 'manage_network' : 'manage_options';
$caps = array_diff($caps, [ $manage_name ]);
}
}
return $caps;
}