I’m having a bit of an issue with an ajax call on a site I’ve just taken over.
I’ve used similar calls before on other sites so not sure if it’s a plugin restriction or conflict but any ideas are welcome!
The call is returning the full content of the page rather than the content the id of the post which is what I expected. I have added an alert to the js before the ajax call and the “thisPost” var is set correctly. I’ve tried a variety of different methods but none seem to let me return the post_id as a json object. In the end the return will be a json object with more details than just the post_id but cut it down till I get it working.
functions.php
function add_calendar_scripts(){
if (is_page_template('template-eventsplanner.php') ) {
wp_register_script('calendar', ( get_bloginfo('template_url') . '/js/calendar.js'), array ('jquery'));
wp_enqueue_script('calendar');
wp_localize_script( 'calendar', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
}
add_action('wp_head', 'add_calendar_scripts');
function get_full_event_callback(){
global $wpdb;
$response = array();
$post_id = intval($_POST['posted']);
$response[] = array('id'=>$post_id);
$result = json_encode($response);
echo "post_id";
die();
}
add_action( 'wp_ajax_nopriv_get_full_event', 'get_full_event_callback' );
add_action( 'wp_ajax_get_full_event', 'get_full_event_callback' );
calendar.js
jQuery(document).ready(function() {
var container = jQuery('body'),
scrollTo = jQuery('.today');
container.animate({
scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop() - 120
});
Shadowbox.init({
skipSetup: true
});
jQuery('.event_box').click( function(){
var pretext = "<h1>Davis Track Hire Job Information</h1>";
var thisPost= jQuery(this).data("event");
var contents, pdf_button;
var data = {
action: 'get_full_event',
dataType: 'json',
posted: thisPost,
};
jQuery.post(MyAjax.ajax_url, data, function(response) {
if(response!=0){
contents = pretext.concat(response);
pdf_button = "<a href='#'>Link to PDF to follow</a>";
// open a welcome message as soon as the window loads
Shadowbox.open({
content: contents.concat(pdf_button),
player: "html",
title: "Event Details",
height: 600,
width: 500
});
}
});
});
});
My issue was simple;
was meant to be:
problem solved… sorry