capabilities – Prevent all users from editing posts except admins using hooks

Question

So I’m attempting to block out all post edits for all users except admins, so I have the following setup:

add_filter('post_row_actions', [self::class, 'remove_edit_permissions'], 10, 2);

public static function remove_edit_permissions(array $actions = []): array
{
    $user = User::init();
    if (is_null($user)) {
        return [];
    }
    if (!$user->role()->has_capability('manage_options')) {
        if (isset($actions['inline hide-if-no-js'])) {
            unset($actions['inline hide-if-no-js']);
        }
        if (isset($actions['edit'])) {
            unset($actions['edit']);
        }
    }

    return $actions;
}

This works great, it blocks out the “Quick Edit” and “Edit” buttons in the Admin Post UI such as below:

enter image description here

Problem:

Users are still able to access the edit url and make changes such as this: https://example.com/wp-admin/post.php?post=10690&action=edit

Is there a way to restrict link editing also?

Is there a much better solutions on editing prevention using some sort of hook or filter?

All help will be appreciated!

0
theMap 4 months 2022-07-24T19:33:41-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse