So, I have a plugin that uses Ajax to dynamically build the post content piece by piece for a custom post type. Once the user has build their page, they hit Update/Publish and Ajax sends the entire built page to a the main plugin PHP file where I am attempting to call wp_insert_post()
.
Here is my relevant code (and yes the var new_content
is set and is correct):
jQuery.ajax({
url: ajaxurl,
method: 'POST',
data : {
update_or_create: '1',
post_content: new_content,
post_title: jQuery('#title').val(),
pid: jQuery('#post_ID').val(),
action : "update_or_save_zen_page"
} ,
success : function(data, textStatus, XMLHttpRequest) {
//alert(data);
console.log(data); // the data sent to the php file is correct
console.log(textStatus); // reads 'success'
console.log(XMLHttpRequest); // normal from what I can tell, "readyState: 4"
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("An unknown error has occurred"); // this does not fire
//console.log(data);
//console.log(textStatus);
//console.log(errorThrown);
//console.log(XMLHttpRequest);
}
});
Here is the php code this is posting to:
function pbfcz_save_zen_page() {
$pid = ( is_numeric($_POST['pid']) ? $_POST['pid'] : 0);
$the_content = sanitize_post_field ('post_content', $_POST['post_content'], $pid);
$the_title = sanitize_text_field ( $_POST['post_title'] );
$post_array = array (
'ID' => $pid,
'post_title' => $the_title,
'post_content' => $the_content, // at this point, $the_content is correct
'post_status' => 'publish',
'post_type' => 'zen_page'
);
$insert = wp_insert_post( $post_array, true ); // this returns the ID every time, meaning successful update, but it is not always successful
//echo $insert; // this is always the post ID, meaning success
echo $the_content; // this is still correc
wp_die();}
When the user selects save, I have tracked the data all the way until the argument for wp_insert_post()
. It is ALWAYS correct. The function ALWAYS returns the post ID, meaning is was successful. I don't get a WP_Error
returned or any HTML code errors like 404 or 500 or anything like that.
I don't get any errors in the chrome developer tools console either. But it updates successfully SOMETIMES - maybe every 1 in 10 tries it works.
I have tried using GET instead of POST
I have tried removing the 'post_status' => 'publish'
from the arguments.
I have tried converting the post_content
to UTF8 by using utf8_encode()
.
I have tried using post_content_filtered
rather than post_content
.
Nothing seems to be working, and it works randomly, and I cannot find a pattern.
The only other piece of information I have that may be related is that images have a hard time loading on this same page. If there is an image in the page content, I get errors such as
"net::ERR_SPDY_PING_FAILED" or "net::ERR_CONNECTION_RESET"
or:
"net::ERR_CONNECTION_CLOSED"
sometimes on page load. The server tries to load the image for over a minute then times out (but the image is already displaying on the page for that entire minute, then when it times out the image disappears).
Can anybody suggest why this is not working (with zero errors), but does work sometimes? Thank you all in advance and let me know if you need more code or background.