admin管理员组

文章数量:1202038

So I am looking at adding some custom post_meta to posts and I want those posts to not be editable, which will be some content that I am pulling in.

Here is what I have:

I have solved the issues when visiting the Post post_type and attempting to edit a post with a specific post_meta using this (This works great in the UI):

add_filter('post_row_actions', function($actions, $post) {
    if (get_post_meta($post->ID, 'global_post', true)) {
        unset($actions['edit']);
        unset($actions['inline hide-if-no-js']);
    }

    return $actions;
}, 10, 2);

The above hides the "edit" and "quick edit" buttons for posts with specific post_meta in the UI.

However, when visiting the edit link directly (Example: .php?post=19771&action=edit - I was still able to edit the post.

Is there a way that I can prevent a post from being edited when visiting a link directly? I was able to successfully hide the buttons in the UI, but this would also be helpful.

So I am looking at adding some custom post_meta to posts and I want those posts to not be editable, which will be some content that I am pulling in.

Here is what I have:

I have solved the issues when visiting the Post post_type and attempting to edit a post with a specific post_meta using this (This works great in the UI):

add_filter('post_row_actions', function($actions, $post) {
    if (get_post_meta($post->ID, 'global_post', true)) {
        unset($actions['edit']);
        unset($actions['inline hide-if-no-js']);
    }

    return $actions;
}, 10, 2);

The above hides the "edit" and "quick edit" buttons for posts with specific post_meta in the UI.

However, when visiting the edit link directly (Example: https://example.com/wp-admin/post.php?post=19771&action=edit - I was still able to edit the post.

Is there a way that I can prevent a post from being edited when visiting a link directly? I was able to successfully hide the buttons in the UI, but this would also be helpful.

Share Improve this question asked May 5, 2022 at 18:25 SemSem 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

I think an elegant way to accomplish this would be to hook into map_meta_cap and deny users as though they do not have the capability to edit the post. This should also gate any other potential work-arounds, such as modifying the post through the REST API or direct POSTing. As an added benefit, it will also take care of omitting the edit links such that you don't need to include your current solution.

add_filter( 'map_meta_cap', 'wpse405452_restrict_global_post_editing', 10, 4 );

function wpse405452_restrict_global_post_editing( $caps, $cap, $user_id, $args ) {
  if( $cap !== 'edit_post' )
    return $caps;
  
  $post_id = $args[0];

  if( get_post_meta( $post_id, 'global_post', true ) )
    $caps[] = 'do_not_allow';
    
  return $caps;
}

An introduction to roles & capabilities can be found in the Plugin Developer Handbook

本文标签: filtersPrevent posts with certain postmeta to be edited