这在块编辑器,甚至是经典编辑器之前就已经存在了,可以在WordPress 2.0中看到。它甚至早于适当的代谢箱!
在块编辑器中,author字段的存在取决于用户是否有操作wp:action-assign-author
, 但没有直接的能力来决定这一点。
如果我们看看authorship plugin 我们看到它可以被删除:
/**
* Filters the post data for a REST API response.
*
* This removes the `wp:action-assign-author` rel from the response so the default post author
* control doesn\'t get shown on the block editor post editing screen.
*
* This also adds a new `authorship:action-assign-authorship` rel so custom clients can refer to this.
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response The response object.
*/
function rest_prepare_post( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) : WP_REST_Response {
$links = $response->get_links();
if ( isset( $links[\'https://api.w.org/action-assign-author\'] ) ) {
$response->remove_link( \'https://api.w.org/action-assign-author\' );
$response->add_link( REST_REL_LINK_ID, $links[\'self\'][0][\'href\'] );
}
return $response;
}
因此,在编辑帖子时,类似这样的操作可能会为所有用户删除它:
/**
* Filters the post data for a REST API response.
*
* This removes the `wp:action-assign-author` rel from the response so the default post author
* control doesn\'t get shown on the block editor post editing screen.
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response The response object.
*/
function rest_prepare_post_remove_author_action( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) : WP_REST_Response {
$links = $response->get_links();
if ( isset( $links[\'https://api.w.org/action-assign-author\'] ) ) {
$response->remove_link( \'https://api.w.org/action-assign-author\' );
}
return $response;
}
add_filter( \'rest_prepare_post\', \'rest_prepare_post_remove_author_action\', 10, 3 );
然后,您可以将其调整为包含角色/能力检查。请注意,这可能只是表面现象,直接向API发出请求或使用classic编辑器的用户仍然可以设置作者。
这只是愚弄了块编辑器和其他行为良好的RESTAPI应用程序,使其相信这是不可能的