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 badges1 Answer
Reset to default 2I 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
版权声明:本文标题:filters - Prevent posts with certain post_meta to be edited 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738600732a2102050.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论