Last active
November 12, 2022 10:13
-
-
Save gaddoz/59e2f4626a73310816b5b864f40556f3 to your computer and use it in GitHub Desktop.
wp-ajax-sample #wp #php
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
<script type="text/javascript"> | |
function doAjax(){ | |
var data1 = jQuery("#data1").val(); | |
var data2 = jQuery("#data2").val(); | |
var myajaxurl = '<?php echo admin_url('admin-ajax.php') ?>'; | |
var data = new FormData(); | |
//parametro action è richiesto da wp-ajax è l'hook usato per il match della funzione (wp_ajax_itc_sample_ajax o wp_ajax_nopriv_itc_sample_ajax) | |
data.append('action','itc_sample_ajax'); | |
data.append('method', 'delet'); | |
data.append('data1', data1); | |
data.append('data2', data2); | |
jQuery.ajax({ | |
url: myajaxurl, | |
data: data, | |
cache: false, | |
contentType: false, | |
processData: false, | |
type: 'POST', | |
error: function(json){ | |
console.log('error'); | |
}, | |
success: function(json){ | |
console.log(json); | |
var data = jQuery.parseJSON( json ); | |
console.log(data); | |
} | |
}); | |
</script> | |
<?php | |
class ItcAjaxSample | |
{ | |
/** | |
* | |
* @var ItcAjaxSample | |
*/ | |
private static $instance = null; | |
/** | |
* | |
* @return ItcAjaxSample | |
*/ | |
public static function get_instance() { | |
if ( null == self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
protected function __construct() { | |
add_shortcode( 'itc_ajax_sample',array($this,'do_ajax_sample'), 10, 2); | |
add_action( 'wp_enqueue_scripts', array($this,'enqueue_script') ); | |
//solo se anche pubblico (no wp-admin) | |
add_action( 'wp_ajax_nopriv_itc_sample_ajax', array($this,'itc_sample_ajax') ); | |
//per wp-admin | |
add_action( 'wp_ajax_itc_sample_ajax', array($this,'itc_sample_ajax') ); | |
} | |
public function do_ajax_sample($atts, $content = null ) { | |
$a = shortcode_atts( array( | |
'attr1' => null, | |
), $atts ); | |
$attr1 = $a["attr1"]; | |
$path = plugin_dir_path(__FILE__) . 'templates/'; | |
//composer "league/plates": "^3.3" | |
$templates = new League\Plates\Engine( $path , "tpl"); | |
$data = []; | |
$data['title'] = "PROVA"; | |
$data['ajaxurl'] = admin_url('admin-ajax.php'); | |
$html = $templates->render('itc-pbx-innovaphone-reporting', $data); | |
echo $html; | |
} | |
public function enqueue_script() { | |
wp_enqueue_script( 'itc_sample1', plugins_url() . '/myplugin/js/sample.js', array( 'jquery' ), '1.1.1', false ); | |
wp_enqueue_script( 'itc_sample2', plugins_url() . '/myplugin/js/sample.js', array( 'jquery' ), '1.2.2', false ); | |
} | |
public function itc_sample_ajax(){ | |
echo json_encode($_POST); | |
die(); | |
} | |
} | |
$ItcPbxInnovaphoneReporting = ItcPbxInnovaphoneReporting::get_instance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment