Create a gist now

Instantly share code, notes, and snippets.

add_action("wp_enqueue_scripts", "my_enqueue", 11);
function my_enqueue() {
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://", false, null);
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment