Skip to content

Instantly share code, notes, and snippets.

@scottpdawson
Last active March 27, 2017 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottpdawson/9fecc876c198f3a1eaf7bb61568294ca to your computer and use it in GitHub Desktop.
Save scottpdawson/9fecc876c198f3a1eaf7bb61568294ca to your computer and use it in GitHub Desktop.
<?php
add_action("wp_enqueue_scripts", "my_enqueue", 11);
function my_enqueue() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
wp_enqueue_script( 'function', get_stylesheet_directory_uri().'/js/gallery_script.js', 'jquery', true);
}
// add actions to respond to get_gallery_data
add_action("wp_ajax_nopriv_get_gallery_data", "get_gallery_data");
add_action("wp_ajax_get_gallery_data", "get_gallery_data");
// define the get_gallery_data function that retrieves records from the database
function get_gallery_data() {
if ( !wp_verify_nonce( $_REQUEST['nonce'], "my_gallery_nonce")) {
exit("No naughty business please");
}
// build your SQL up using the request parameters, for example, $_REQUEST["occasion_id"]
$query = "SELECT id, occasion, size FROM cakes WHERE occasion = " . $_REQUEST["occasion_id"];
// issue the query
global $wpdb;
$query_result = $wpdb->get_results($query, OBJECT);
$result['type'] = "success";
$result['data'] = $query_result;
// encode result as JSON and return it
$result = json_encode($result);
echo $result;
die();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment