Created
April 13, 2013 06:15
-
-
Save anderson-mota/5377252 to your computer and use it in GitHub Desktop.
Script para executar Ajax no wp
This file contains hidden or 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
<?php | |
function scriptAjax($id_form, $resposta){ | |
?> | |
<script type="text/javascript"> | |
var $jquery = jQuery.noConflict(); | |
$jquery(document).ready(function($) { | |
$('#<?php echo $id_form; ?>').submit(function(e) { | |
e.preventDefault(); | |
var img = "<img src='<?php bloginfo('template_url'); ?>/images/ajax-loader.gif' alt='Aguarde...' />"; | |
$('#<?php echo $resposta; ?>').html( img ); | |
$.ajax({ | |
type: 'POST', | |
url: '<?php echo admin_url('admin-ajax.php'); ?>', | |
data: $(this).serialize(),//conteúdo do form (variaveis POST), inclusive action = nome-da-função | |
success: function( response ){ | |
$('#<?php echo $resposta; ?>').html( response ); | |
} | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
function adminConsultaUnView() { | |
/* global $wpdb; | |
$min = 0; | |
$max = 20; | |
$resultados = $wpdb->get_results("SELECT id, n_onu, nome FROM produtos_ibcs WHERE status = 1 LIMIT $min, $max"); | |
*/ | |
load_template( TEMPLATEPATH . '/DataTables/addingrecords.php'); | |
} | |
// Create the function use in the action hook | |
function addAdminConsultaUn() { | |
wp_add_dashboard_widget('adminConsultaUn', 'Gerenciamento Consulta Un', 'adminConsultaUnView'); | |
} | |
// Hoook into the 'wp_dashboard_setup' action to register our other functions | |
add_action('wp_dashboard_setup', 'addAdminConsultaUn' ); | |
function consultaUn(){ | |
global $wpdb; | |
$onu = $_POST['onu']; | |
$loop = new WP_Query("pagename=consulta-un"); | |
while ( $loop->have_posts() ) { | |
$loop->the_post(); | |
$resultados = $wpdb->get_results("SELECT n_onu, nome FROM produtos_ibcs WHERE status = 1 AND n_onu = " . $onu); | |
foreach($resultados as $resultado) { | |
?> | |
<p> | |
<?php pll_e("Número UN:") ?> <?php echo $resultado->n_onu ?><br/> | |
<?php pll_e("Nome:") ?> <?php echo $resultado->nome ?> | |
</p> | |
<?php | |
//_e("<p>Número UN: %s <br/>Nome: %s </p>", $resultado->n_onu, $resultado->nome); | |
the_content(); | |
$achou = true; | |
} | |
if(!$achou){ | |
the_excerpt(); | |
} | |
} | |
die(); | |
} | |
add_action('wp_ajax_consultaUn', 'consultaUn'); | |
add_action('wp_ajax_nopriv_consultaUn', 'consultaUn'); | |
function consultaUnView(){ | |
scriptAjax("consulta_un", "resposta"); | |
?> | |
<form action="" method="post" id="consulta_un"> | |
<input type="hidden" name="action" value="consultaUn" /> | |
<label><?php pll_e("Número UN:") ?> <input type="text" name="onu" value="" /></label> <input type="submit" value="<?php pll_e("Consultar") ?>"> | |
</form> | |
<div id="resposta"></div> | |
<?php | |
} | |
add_shortcode('consultaun', 'consultaUnView'); | |
?> | |
<?php | |
/** | |
* Segue abaixo o código para inserir no thema do wp: | |
*/ | |
?> | |
<form action="" method="post" id="consulta_un"> | |
<input type="hidden" name="action" value="consultaUn" /> | |
<label>Número UN: <input type="text" name="onu" value="" /></label> <input type="submit" value="Consultar"> | |
</form> | |
<div id="resposta"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function registerUser() {
}
add_action('wp_ajax_consultaUn', 'registerUser');
add_action('wp_ajax_nopriv_consultaUn', 'registerUser');