Wordpress apply add_filter('user_has_cap') when update a post

Viewed 300

WordPress version 5.4.2

I have a custom filter to modify editor capabilities temporarily when he tried to update a post, the filter is working when the editor tried to update an existing post. but there is a problem after the editor click the update button,

after its finish the update, the button will be showing submit for review but what I want is stay with update button, because what I'm trying to do is,

when editor upload a new post, admin must be approve it first, then after that the post that has been approved can be edited and published by the editor, no need for the admin permission anymore.

and here is my filter.

add_filter('user_has_cap', function ($allcaps) {
    if (!is_user_logged_in()) return $allcaps;
    
    $user = wp_get_current_user();
    $roles = (array) $user->roles;
    if (!in_array('editor', $roles)) return $allcaps;
    global $pagenow, $post;

    if (in_array($pagenow, array('post.php', ))) {
        $post_type = get_post_type($post->ID);
        if ($post_type == 'post') {
            $allcaps['publish_posts'] = 1;
        }
    }

    return $allcaps;
}, 3);

but and what I've tried for the ajax request.

add_action('pre_post_update', function($post_ID, $data) {
    add_filter('user_has_cap', function ($allcaps) {
        ... // exactly the same with the filter code
    }, 3);
});

so how to achieve what I want for after editor make the update on existing post? so the button will be stays in update not submit for review. thankyou

0 Answers
Related