Created
April 4, 2022 01:35
-
-
Save daniloroddrigues/be52e35f6bfd2bdd237316fa1c8ffe16 to your computer and use it in GitHub Desktop.
Filter Wordpress Php Ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function nb_filter_search() | |
{ | |
wp_enqueue_script("js_nb_filter_search", get_stylesheet_directory_uri() . '/js/nbfilter.js', array('jquery'), '1.0', true); | |
wp_localize_script("js_nb_filter_search", 'ajax_nb_filter_search', array('ajax_url' => admin_url('admin-ajax.php'))); | |
} | |
add_action('wp_enqueue_scripts', 'nb_filter_search'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function ($) { | |
$('form.ajax').on('submit', function (e) { | |
e.preventDefault(); | |
var filter = $('#filter'); | |
$.ajax({ | |
url: ajax_nb_filter_search.ajax_url, | |
type: filter.attr('method'), // POST | |
data: filter.serialize(), | |
dataType: 'json', | |
beforeSend: function (xhr) { | |
filter.find('button').text('Processando...'); | |
}, | |
success: function (resp) { | |
filter.find('button').text('Procurar'); | |
$('#nb').html(''); | |
$('#nome').html(''); | |
$('#imagem').attr('src', ''); | |
$('#error').html(''); | |
if (resp.error) { | |
$('#error').html(resp.error_message); | |
} else { | |
$('#nb').html(resp[0].nb); | |
$('#nome').html(resp[0].nome); | |
$('#imagem').attr('src', resp[0].imagem); | |
} | |
}, | |
error: function (req, error) { | |
filter.find('button').text('Procurar'); | |
$('#response').html(error); | |
} | |
}); | |
$('.ajax')[0].reset(); | |
return false; | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* nb filter form | |
* | |
*/ | |
function nb_filter_form() | |
{ | |
$nb = remove_sp_chr(stripslashes($_POST['nb'])); | |
if (empty($nb)) { | |
$result['error'] = true; | |
$result['error_message'] = 'O numero do beneficiário não foi encontrado ou ainda não foi cadastrado no sistema, por favor entrar em contato com nosso suporte!'; | |
echo wp_json_encode($result); | |
die(); | |
} else { | |
$args = array( | |
'numberposts' => -1, | |
'post_type' => 'acompanhamento', | |
'meta_key' => 'nb', | |
'meta_value' => $nb | |
); | |
$search_query = new WP_Query($args); | |
if ($search_query->have_posts()) { | |
$result = array(); | |
// loop | |
while ($search_query->have_posts()) { | |
$search_query->the_post(); | |
$result[] = array( | |
"id" => get_the_ID(), | |
"title" => get_the_title(), | |
"nb" => get_field('nb'), | |
"nome" => get_field('nome'), | |
"imagem" => get_field('imagem') | |
); | |
} | |
echo wp_json_encode($result); | |
} else { | |
// no posts found | |
$result['error'] = true; | |
$result['error_message'] = 'O numero do beneficiário não foi encontrado ou ainda não foi cadastrado no sistema, por favor entrar em contato com nosso suporte!'; | |
echo wp_json_encode($result); | |
} | |
} | |
die(); | |
} | |
add_action('wp_ajax_nb_filter_form', 'nb_filter_form'); | |
add_action('wp_ajax_nopriv_nb_filter_form', 'nb_filter_form'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment