Skip to content

Instantly share code, notes, and snippets.

@rajucs
Created April 27, 2020 17:58
Show Gist options
  • Save rajucs/982e486661adc36e4c2cddde52dd847d to your computer and use it in GitHub Desktop.
Save rajucs/982e486661adc36e4c2cddde52dd847d to your computer and use it in GitHub Desktop.
// ajax used in front end
add_action( 'init', 'dsm_script_enquer');
function dsm_script_enquer() {
wp_register_script( "dsm_js_front", get_template_directory_uri().'/js/main.js', array('jquery') );
wp_localize_script( 'dsm_js_front', 'dsmAjax',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajax_nonce' => wp_create_nonce('ajax_csrf_check'),
));
wp_enqueue_script( 'dsm_js_front' );
// wp_enqueue_script( 'jquery' );
}
add_action( 'wp_ajax_dsmGetCustomPostTypes', 'dsmGetCustomPostTypes' );
function dsmGetCustomPostTypes() {
ob_clean(); //TO CLEAN EXTRA SPACE
$class_id = $_POST["class_id"];
$security = $_POST["security"];
// print_r($security); //cefd6888dc
if(check_ajax_referer( 'ajax_csrf_check', 'security' ) ){
$user_id = get_current_user_id();
global $wpdb;
// check this address is his or not
$data = [];
$admin_post_types = $wpdb->prefix . 'dsm_custom_post_types';
$post_type = $wpdb->get_results( "SELECT * FROM $admin_post_types WHERE class_id = '".$class_id."'");
foreach($post_type as $type)
{
$single_data = [];
$single_data["post_type_slug"] = $type->post_type_slug;
$single_data["post_type_name"] = $type->post_type_name;
$single_data["id"] = $type->id;
array_push($data, $single_data);
}
echo json_encode($data);
}else{
echo 'csrfAttack';
}
wp_die(); // this is required to terminate immediately and return a proper response
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment