我从Wordpress获得以下PHP错误:
Warning: Cannot modify header information - headers already sent by (output started at domain/public_html/sdapi/wp-content/plugins/testpost/testpost.php:74) in domain/public_html/sdapi/wp-admin/post.php on line 235
Warning: Cannot modify header information - headers already sent by (output started at domain/public_html/sdapi/wp-content/plugins/testpost/testpost.php:74) in domain/public_html/sdapi/wp-includes/pluggable.php on line 1196
使用此代码:
<?php
/**
* Plugin Name: testpost
*/
add_action( 'publish_post', 'testpost', 10, 1 );
function testpost( $post ) {
// ***** Get variables *****
$post_id = $post;
$post_object = get_post( $post_id );
$post_object->post_content;
$title = $post_object->post_title;
$source_url = get_permalink( $post_id );
$body = $post_object->post_content;
$titleprint = preg_replace("/\r?\n/", "\\n", addslashes($title));
$bodyprint = preg_replace("/\r?\n/", "\\n", addslashes($body));
$posturlprint = preg_replace("/\r?\n/", "\\n", addslashes($source_url));
// ***** Run script *****
echo "<script>
var Request = new XMLHttpRequest();
Request.open('POST', '****POSTURL****');
Request.setRequestHeader('Content-Type', 'application/json');
Request.setRequestHeader('Accept', 'application/json');
Request.setRequestHeader('Authorization', '****TOKEN****');
Request.onreadystatechange = function () {
if (this.readyState === 4) {
}
};
var body = {
'article': {
'uuid': '090bda74-b021-4c7c-a44a-44f33bba32142',
'title': '". $titleprint ."',
'source_url': '". $posturlprint ."',
'body': '". $bodyprint ."'
}
};
Request.send(JSON.stringify(body));
</script>";
}
?>
*通过粘贴添加<?php
之前的空格,无法摆脱它:(
起初我认为正是这里广泛解释的空白问题:How to fix "Headers already sent" error in PHP。
不幸的是,它不是(易于修复)而且我认为这是“打印,回声问题”,如同一个答案所述。
不幸的是我需要使用echo,否则我的插件根本不起作用。
是否有替代方法可以在这里使用echo,或者可能是某种方式? 希望有人可以提供帮助。
答案 0 :(得分:0)
似乎在代码的开头存在空格。这就是你得到这个警告的原因。在php标签的开头和结尾不应该有任何空格。
答案 1 :(得分:0)
尝试使用ob_start();
来阻止标头已发送问题。这个问题主要是由于PHP代码中有额外的空格,或者在关闭?>
标记之前你有太多的空格,你也可以避免使用这个结束标记,请删除这些空格。
<?php
ob_start();
/**
* Plugin Name: testpost
*/
add_action( 'publish_post', 'testpost', 10, 1 );
function testpost( $post ) {
// ***** Get variables *****
$post_id = $post;
$post_object = get_post( $post_id );
$post_object->post_content;
$title = $post_object->post_title;
$source_url = get_permalink( $post_id );
$body = $post_object->post_content;
$titleprint = preg_replace("/\r?\n/", "\\n", addslashes($title));
$bodyprint = preg_replace("/\r?\n/", "\\n", addslashes($body));
$posturlprint = preg_replace("/\r?\n/", "\\n", addslashes($source_url));
// ***** Run script *****
echo "<script>
var Request = new XMLHttpRequest();
Request.open('POST', '****POSTURL****');
Request.setRequestHeader('Content-Type', 'application/json');
Request.setRequestHeader('Accept', 'application/json');
Request.setRequestHeader('Authorization', '****TOKEN****');
Request.onreadystatechange = function () {
if (this.readyState === 4) {
}
};
var body = {
'article': {
'uuid': '090bda74-b021-4c7c-a44a-44f33bba32142',
'title': '". $titleprint ."',
'source_url': '". $posturlprint ."',
'body': '". $bodyprint ."'
}
};
Request.send(JSON.stringify(body));
</script>";
}
?>