Admin AJAX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//==================== function check_previous_videos() { $output = array("exists" => false); // The $_REQUEST contains all the data sent via ajax if ( isset($_REQUEST) ) { $video_name = $_REQUEST['video_name']; $not_in = $_REQUEST['post_id']; $output ['not in'] = $not_in ; $query_array = array( "post_type" => "post", "meta_key" => "video", "meta_value" => $video_name, "post__not_in" => array($not_in)); $myquery = new WP_Query( $query_array); if($myquery->have_posts()){ $output ['exists'] = true; } } echo json_encode($output ); // Always die in functions echoing ajax content die(); } add_action( 'wp_ajax_check_previous_videos', 'check_previous_videos' ); function check_old_videos($hook) { wp_enqueue_script( 'my_custom_script', get_stylesheet_directory_uri(). '/js/sctipt.js' ); } add_action( 'admin_enqueue_scripts', 'check_old_videos' ); //==================== |
sctipt.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
$ = jQuery; jQuery(document).ready(function($) { // Validate New Password fields $("#acf-field-video").blur(function() { var __that = $(this); $("#publish").prop('disabled', true); $.ajax({ url: ajaxurl, data: { 'action': 'check_previous_videos', 'video_name': $("#acf-field-video").val(), 'post_id' :$("#post_ID").val() }, success: function(data) { // This outputs the result of the ajax request console.log(data); try { var obj = JSON.parse(data); if (obj.exists) { // $("#publish").prop('disabled', false); __that.css("border-color", "#ff0000"); } else { $("#publish").prop('disabled', false); __that.css("border-color", "#008000"); } } catch (e) {} }, error: function(errorThrown) { console.log(errorThrown); } }); }); }); |