【发布时间】:2021-12-28 22:55:21
【问题描述】:
5 年前,我花钱请人为我的网站制作插件,现在我正试图让它再次工作。我不知道 JSON 是如何工作的,所以这是我第一次尝试它。基本上,我的插件会使用 Iframely API 来获取文章摘要,并将数据从编辑器中的 URL 保存到自定义字段。
我在function.php 中有问题的代码是这样的:
function post_extra_save( $post_id, $post){
global $pagenow;
if ($pagenow == 'post.php') {
if ( has_post_format('link', $post_id)) {
$url = get_post_field('post_content', $post_id);
$request_url = 'https://iframe.ly/api/iframely?url='. urlencode($url) .'&api_key='.get_field_object('api_key', 'option')['value'];
$response = wp_remote_get( esc_url_raw( $request_url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
update_field('field_61942d74195e7', $api_response);
}
}
}
add_action( 'save_post', 'post_extra_save', 10, 2 );
它的作用是将 URL 和 API 密钥发送到 Iframely API,返回 JSON 并将原始输出保存到自定义字段。
当我保存帖子时,我收到以下错误消息:
Warning: htmlspecialchars() expects parameter 1 to be string, array given in /wp-includes/formatting.php on line 4529
知道这意味着什么吗?
【问题讨论】:
-
都没有用,是的,我确实问了另一个问题,但我收到一条新的错误消息并将其删除并将其重写为该主题。
标签: json wordpress wordpress-theming advanced-custom-fields