Have been trying to solve this for couple of hours already. Here's the case: I'm making a plugin that separates the posts into pages using the <!--nextpage-->
tag. It works fine but I want to change the pages via ajax.
Below I'm showing how I do it but when the content is returned the_content filters are not applied and many functionalities are not showing (the pagination which I added and probably theme and other plugins functionality that is attached to the post).
Here's my ajax function:
jQuery(document).ready(function($) {
$(document).on('click', '#upp-pagination a', function(event) {
event.preventDefault();
var page = $(this).parent().attr('class').replace(/\D/g, '');
var postID = $('#upp-pagination').attr('class').replace(/\D/g, '');
$.ajax({
url: upppagination.ajaxurl,
type: 'post',
data: {
action: 'load_page',
page: page,
postID: postID
},
success: function (result) {
var newPage = result;
$('div.entry-content').html(newPage);
}
});
});
});
And this is the function on the back-end that returns the pages:
public function loadPage() {
$postID = $_POST['postID'];
$page = $_POST['page'];
$post = get_post($postID);
$content = $post->post_content;
if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
$content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
$content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
$content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
// Ignore nextpage at the beginning of the content.
if ( 0 === strpos( $content, '<!--nextpage-->' ) )
$content = substr( $content, 15 );
$pages = explode('<!--nextpage-->', $content);
} else {
$pages = array( $post->post_content );
}
$page = $pages[$page - 1];
$response = apply_filters('the_content', $page);
wp_send_json($response);
}
Am I using the right approach here and how I can apply the filters? Any ideas?
is_single
won't work in AJAX - it's a separate request that has no awareness of the query-type from the page that called it.