0

I'm getting the following PHP error's from Wordpress:

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

With this code:

    <?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>";




}
?>

*Whitespace before <?php is added by pasting here, cannot get rid of it :(

At first I thought it was the Whitespace problem as explained extensively here: How to fix "Headers already sent" error in PHP.

Unfortunately that's not it (easy to fix) and I'm thinking it's the "Print, echo issue" as described in the same answer.

Unfortunately I need to use echo, otherwise my plugin doesn't work at all.

Is there an alternative for using echo here, or perhaps some way around this? Hope someone can help.

Community
  • 1
  • 1
joren
  • 89
  • 1
  • 9
  • Try by using ob_start(); at the starting of your php script – Sachin I Jun 12 '15 at 15:19
  • But you have whitespace right on your first line...before the `` php tag in your file – rnevius Jun 12 '15 at 15:19
  • Also, the error appears to be referring to line 74...but you don't have a line 74. Is that your whole plugin file? – rnevius Jun 12 '15 at 15:31
  • I think there is WordPress API function you should use instead of `echo()` but I don't know it. You better ask on http://wordpress.stackexchange.com. – axiac Jun 12 '15 at 15:38
  • Thank you for your replies! - Sachin: ob_start() prevents the rest of the code from running (see below) - rnevius: The whitespace is indeed created by pasting the code here, I'll add that to the question. And this is indeed the whole plugin file. I don't see the Error mentioning line 74. I do have line 74, but that's just in the middle of the js... – joren Jun 14 '15 at 09:39

2 Answers2

0

It seems like whitespace exists in the start of your code. That's why you are getting this warning. There should not be any space in the beginning and end of the php tags.

  • The whitespace is added because of pasting here. It's not in the file, I should have mentioned that! – joren Jun 14 '15 at 09:45
0

Try to use ob_start(); to prevent headers already sent problem. this problem is mostly due to extra spaces in php code or and also you have too much spaces before closing the ?> tag , you can also avoid to use this closing tag and please remove these spaces.

<?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>";
}
?>
Arsh Singh
  • 1,580
  • 1
  • 11
  • 31
  • Thanks for the suggestion Arsh. This does get rid of the errors but unfortunately prevents the rest of the code from running, should ob_start() do that? – joren Jun 14 '15 at 09:38
  • No ob_start(); will not stop your code from running. – Arsh Singh Jun 14 '15 at 14:11
  • Unfortunately my script did not run when i added ob_start(). As I understand this is because the redirect I made in the script is ignored by ob_start(). I decided to go another way; I've added a button in the cms to send the post, now the headers aren't mixed up. Thank you for your help :) – joren Jun 15 '15 at 10:28