Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anthonyboutinov/3fe56de68acedf9be7c162b8730f09a9 to your computer and use it in GitHub Desktop.
Save anthonyboutinov/3fe56de68acedf9be7c162b8730f09a9 to your computer and use it in GitHub Desktop.
Allow editors in Wordpress to edit Privacy Policy page
// Source: https://wordpress.stackexchange.com/questions/318666/how-to-allow-editor-to-edit-privacy-page-settings-only
// 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)
{
$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;
}
@tombroucke
Copy link

$user_meta can be false, so you need to check that, otherwise you will get a fatal error for some requests. You should add a guard clause:

if (!$user_meta) {
    return $caps;
}

Some refactoring to make it more readable:

add_action('map_meta_cap', function($caps, $cap, $user_id, $args) {
    $user_meta = get_userdata($user_id);
    
    if (!$user_meta) {
        return $caps;
    }

    if ('manage_privacy_options' !== $cap) {
        return $caps;
    }
    
    if (!array_intersect(['editor', 'administrator'], $user_meta->roles)) {
        return $caps;
    }

    $manage_name = is_multisite() ? 'manage_network' : 'manage_options';
    $caps = array_diff($caps, [ $manage_name ]);
    return $caps;
}, 1, 4);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment