当我向custom get\\u all\\u posts API回调添加过滤器时,API可以工作,但每个页面的管理页面都是空白的。为什么?
function get_all_posts( $data, $post, $context ) {
return [
\'id\' => $data->data[\'id\'],
\'date\' => $data->data[\'date\'],
\'date_gmt\' => $data->data[\'date_gmt\'],
\'modified\' => $data->data[\'modified\'],
\'title\' => $data->data[\'title\'][\'rendered\'],
\'content\' => $data->data[\'content\'][\'rendered\'],
\'main_text\' => $data->data[\'main_text\'],
\'featured_media_url\' => $data->data[\'jetpack_featured_media_url\'],
\'category\' => get_the_category_by_ID( $data->data[\'categories\'][0] ),
\'link\' => $data->data[\'link\']
];
}
add_filter( \'rest_prepare_post\', \'get_all_posts\', 10, 3 );
EDIT:网页上出现此错误
Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating \'e.length\')
仅供参考:我在post admin页面上添加了一个元框,没有上面的代码,元框就可以完美工作。
我的全部代码:
function main_text_meta_box() {
add_meta_box(
\'main-text\',
__( \'Main text\', \'sitepoint\' ),
\'main_text_meta_box_callback\',
\'post\'
);
}
function main_text_meta_box_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( \'main_text_nonce\', \'main_text_nonce\' );
$value = get_post_meta( $post->ID, \'_main_text\', true );
echo \'<textarea style="width:100%; height:300px;" id="main_text" name="main_text">\' . esc_attr( $value ) . \'</textarea>\';
}
add_action( \'add_meta_boxes\', \'main_text_meta_box\' );
function save_main_text_meta_box_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST[\'main_text_nonce\'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[\'main_text_nonce\'], \'main_text_nonce\' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don\'t want to do anything.
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user\'s permissions.
if ( isset( $_POST[\'post_type\'] ) && \'page\' == $_POST[\'post_type\'] ) {
if ( ! current_user_can( \'edit_page\', $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( \'edit_post\', $post_id ) ) {
return;
}
}
/* OK, it\'s safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST[\'main_text\'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST[\'main_text\'] );
// Update the meta field in the database.
update_post_meta( $post_id, \'_main_text\', $my_data );
}
add_action( \'save_post\', \'save_main_text_meta_box_data\' );
function add_custom_fields() {
register_rest_field(
\'post\',
\'main_text\', //New Field Name in JSON RESPONSEs
array(
\'get_callback\' => \'get_main_text\', // custom function name
\'update_callback\' => null,
\'schema\' => null,
)
);
}
add_action( \'rest_api_init\', \'add_custom_fields\' );
function get_main_text ( $post, $field_name, $request ){
return get_post_meta($post[\'id\'], \'_main_text\', true);
}
SO网友:honk31
迟到了,但遇到了同样的问题,找不到答案,但找到了答案:
它不起作用,因为您破坏了wordpess期望的其余答案。gutenberg还使用REST api,并且使用的过滤器也在编辑器中执行。如果要编辑响应,但仅针对前端,则需要添加一个条件来检查backend vs. frontend
wordpress具有内置功能is_admin()
函数进行检查,但此函数在REST请求中不起作用。但您仍然可以根据上下文检查REST请求的来源:
function so326688_rest_prepare_post($response, $post, $request)
{
if ($request[\'context\'] === \'view\') { // if not is admin
//your code goes here and will only execute on view requests. inside admin the request would be \'edit\'
}
return $response;
}
add_filter(\'rest_prepare_post\', \'so326688_rest_prepare_post\', 10, 3);
除此之外,我要说的是,覆盖整个响应是不好的做法。但你可以添加你的属性。不太确定您在响应中更改了哪些数据,但这应该作为一个示例:
function so326688_2_rest_prepare_post($response, $post)
{
if (isset($post)) {
$response->data[\'category\'] = get_the_category_by_ID($response->data[\'categories\'][0]);
}
return $response;
}
add_filter(\'rest_prepare_project\', \'so326688_2_rest_prepare_post\', 10, 2);
但你的问题肯定是,你改变了整个回答,所以古腾堡丢失了一些值,或者在正确的地方找不到它们。