<?php
// The two following lines belong into the theme's functions.php or wherever scripts are
// embedded for Plugins or Widgets. "portfolio-page.js" is the script I want to make
// Ajax calls from.
// This is the wordpress way of embedding scripts into a page.
wp_enqueue_script('portfolio-page', get_template_directory_uri() . '/js/portfolio-page.js', array('jquery'), false, false);
// This means: "Allow portfolio-page to call stuff with Ajax."
wp_localize_script('portfolio-page', 'ajaxAdmin', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
// These two lines belong into functions.php as well. They map the Ajax calls to the respective
// Php methods. It's important that the parameters match exactly, e.g. here "tne_load_post_content"
// (yes, including the wp_ stuff).
add_action( 'wp_ajax_tnet_load_post_content', 'tnet_load_post_content' );
add_action( 'wp_ajax_nopriv_tnet_load_post_content', 'tnet_load_post_content' );
// This is the actual method that is called from the Javascript.
function tnet_load_post_content()
{
$id = $_POST['pageId']; // Parameters sent by Javascript can be accessed like this.
// Here it is checked, if the current page is one with a Siteorigin layout.
$meta = get_post_meta( $id, 'panels_data', true );
if( class_exists( 'SiteOrigin_Panels' ) && $meta )
{
// Yes: Use Siteorigin magic to get the layout.
$renderer = SiteOrigin_Panels::renderer();
$content = $renderer->render( $id );
}
else
{
// No: Get the layout the standard Wordpress way.
$content = apply_filters( 'the_content', get_page($id)->post_content );
}
// Hand the layout back to the caller.
echo $content;
// With this wordpress will at a "0" in the response.
die();
}
?>