WordPress "Save Draft" 失败:"the response is not a valid JSON response"
Wordpress "Save Draft" fails: "the response is not a valid JSON response"
每次尝试保存我的草稿或发布或许多其他操作(以及所有新闻)时,我都会遇到此错误:
Updating failed. Error message: The response is not a valid JSON response.
要求:
{"id":488,"content":"<!-- wp:paragraph -->\n<p>Try </p>\n<!-- /wp:paragraph -->","title":"Try"}
响应摘录,查看网络选项卡(出于某些奇怪的原因,除了 json 之外,它还包括 html):
<span class="posted-on">Posted on
...
</span>{"id":488,"date":"2020-05-08T22:57:44","date_gmt":"2020-05-08T21:57:44","guid":{"rendered":
...
用于其他问题的解决方案是使用旧的 Classic Editor
但我更喜欢新的:
这个错误的原因很愚蠢。问题是当 wordpress 发送 JSON 请求时,答案不是 JSON 格式。在我的一个主题函数中有一个 echo
而不是 return
,在 template-tags.php.
下
这导致每个 json 都带走了那个 html。
if ( ! function_exists( 'my_posted_on' ) ) {
function my_posted_on() {
$time_string = '';
// ...
$posted_on = apply_filters(
'my_posted_on', sprintf(
'<span class="posted-on">...</span>',
esc_html_x( 'Posted on', 'post date', 'theme' ),
esc_url( get_permalink() ),
apply_filters( 'my_posted_on_time', $time_string )
)
);
$byline = apply_filters(
'my_posted_by', sprintf(
'...'
)
);
echo $posted_on . $byline;
}
}
我用 return
替换了 echo
并修复了它。
更新您的固定链接。
如果未修复禁用块编辑器并切换回经典编辑器。
@aepifano 说得对!我在尝试保存 post:
时遇到此错误
Updating failed. The response is not a valid JSON response.
我正在测试具有此代码的插件:
function plugin_b_shortcode()
{
$latest = '';
$content = '';
if(function_exists('plguin_a_latest_post'))
{
$latest = plguin_a_latest_post();
}
if(is_array($latest) && !empty($latest))
{
echo "<pre>";print_r($latest);echo "</pre>";
$newdate = $latest['post_date'];
$newdate = date("Y-m-d H:i:s",strtotime($newdate.' -7 hours '));
$newdate = date("D M j G:i:s T Y",strtotime($newdate));
$content .= '<div style="padding:22px; border:solid 1px #000; background-color:#CCC;">';
$content .= '<h1>Latest Post</h1>';
$content .= '<h2><a href="'.get_permalink($latest['ID']).'">'.$latest['post_title'].'</a></h2>';
$content .= '<p>Date: '.$newdate.'</p>';
$content .= '</div>';
}
return $content;
}
add_shortcode('plugin_a_latest','plugin_b_shortcode');
当我将 echo
更改为 return
时,我能够更新 post 但是在将 echo
更改为 return
。如果有任何其他解决方案,请告诉我们。
每次尝试保存我的草稿或发布或许多其他操作(以及所有新闻)时,我都会遇到此错误:
Updating failed. Error message: The response is not a valid JSON response.
要求:
{"id":488,"content":"<!-- wp:paragraph -->\n<p>Try </p>\n<!-- /wp:paragraph -->","title":"Try"}
响应摘录,查看网络选项卡(出于某些奇怪的原因,除了 json 之外,它还包括 html):
<span class="posted-on">Posted on
...
</span>{"id":488,"date":"2020-05-08T22:57:44","date_gmt":"2020-05-08T21:57:44","guid":{"rendered":
...
用于其他问题的解决方案是使用旧的 Classic Editor
但我更喜欢新的:
这个错误的原因很愚蠢。问题是当 wordpress 发送 JSON 请求时,答案不是 JSON 格式。在我的一个主题函数中有一个 echo
而不是 return
,在 template-tags.php.
这导致每个 json 都带走了那个 html。
if ( ! function_exists( 'my_posted_on' ) ) {
function my_posted_on() {
$time_string = '';
// ...
$posted_on = apply_filters(
'my_posted_on', sprintf(
'<span class="posted-on">...</span>',
esc_html_x( 'Posted on', 'post date', 'theme' ),
esc_url( get_permalink() ),
apply_filters( 'my_posted_on_time', $time_string )
)
);
$byline = apply_filters(
'my_posted_by', sprintf(
'...'
)
);
echo $posted_on . $byline;
}
}
我用 return
替换了 echo
并修复了它。
更新您的固定链接。 如果未修复禁用块编辑器并切换回经典编辑器。
@aepifano 说得对!我在尝试保存 post:
时遇到此错误Updating failed. The response is not a valid JSON response.
我正在测试具有此代码的插件:
function plugin_b_shortcode()
{
$latest = '';
$content = '';
if(function_exists('plguin_a_latest_post'))
{
$latest = plguin_a_latest_post();
}
if(is_array($latest) && !empty($latest))
{
echo "<pre>";print_r($latest);echo "</pre>";
$newdate = $latest['post_date'];
$newdate = date("Y-m-d H:i:s",strtotime($newdate.' -7 hours '));
$newdate = date("D M j G:i:s T Y",strtotime($newdate));
$content .= '<div style="padding:22px; border:solid 1px #000; background-color:#CCC;">';
$content .= '<h1>Latest Post</h1>';
$content .= '<h2><a href="'.get_permalink($latest['ID']).'">'.$latest['post_title'].'</a></h2>';
$content .= '<p>Date: '.$newdate.'</p>';
$content .= '</div>';
}
return $content;
}
add_shortcode('plugin_a_latest','plugin_b_shortcode');
当我将 echo
更改为 return
时,我能够更新 post 但是在将 echo
更改为 return
。如果有任何其他解决方案,请告诉我们。