I am currently creating a wordpress page based on the X Theme which utilises Cornerstone as a page builder.
I am trying to dynamically load pages into the main page with AJAX. For this I created a script making a request to admin-ajax.php
which works flawlessly.
Now the tricky part: I added a PHP snippet with the plugin Code Snippets containing the following code:
function get_case() {
global $wpdb;
$postNo = intval( $_POST['case'] );
$post = get_post($postID);
$content = apply_filters('the_content', $post->post_content);
$html = do_shortcode($content)
echo $html;
wp_die();
}
add_action( 'wp_ajax_get_case', 'get_case' );
add_action( 'wp_ajax_nopriv_get_case', 'get_case' );
This code returns correct results if I am using the wordpress page builder, however it simply returns the shortcodes of the theme if the page was built in Cornerstone.
e.g.
[cs_element_section _id=”1″][cs_element_row _id=”2″]...
Am I doing this correctly? Shouldn't do_shortcodes() resolve those shortcodes? Is there any other way to dynamically load sub pages?
Any help is appreciated.
You actually don't need do_shortcode
(or global $wpdb
if this is your full code) because that is run automatically on the_content assuming it wasn't unhooked elsewhere. We can't see your full set-up (plugins, theme, custom code) to fully debug, but this code below is all you would need on a Plain Vanilla WordPress site to accomplish what you're trying to do. If this doesn't work, the problem is elsewhere in your set-up:
function get_case() {
$postNo = intval( $_POST['case'] );
$post = get_post($postNo); // NOTE: Your code has the wrong variable here
$content = apply_filters('the_content', $post->post_content);
exit( $content );
}
add_action( 'wp_ajax_get_case', 'get_case' );
add_action( 'wp_ajax_nopriv_get_case', 'get_case' );