-
Notifications
You must be signed in to change notification settings - Fork 12
/
Ajax WP.txt
135 lines (120 loc) · 3.78 KB
/
Ajax WP.txt
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
wp_nonce_field('charity_nonce_action','charity_nonce');?>
$nonce = wp_create_nonce("my_user_vote_nonce");
/////////////////////////////////////////// Function
//Ajax load post
add_action( 'init', 'my_script_enqueuer' );
function my_script_enqueuer() {
wp_register_script( "ajax_load_post", TEMP_URL.'/js/ajax.js', array('jquery') );
wp_localize_script( 'ajax_load_post', 'onego', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'ajax_load_post' );
}
add_action( 'wp_ajax_ajax_load_post', 'ajax_load_post' );
add_action( 'wp_ajax_nopriv_ajax_load_post', 'ajax_load_post' );
function ajax_load_post() {
if ( !wp_verify_nonce( $_REQUEST['nonce'], "my_user_vote_nonce")) {
exit("No naughty business please");
}
die( "Hello World" );
}
///////////////////////////////////// Jquery
$(".viewmore a").click(function(){
$.ajax({
type : "post",
dataType : "json",
url : onego.ajaxurl,
data : {action: "my_user_vote", post_id : post_id, nonce: nonce},
context: this,
beforeSend: function(){
thisWrap.addClass('loading');
},
success: function(response) {
if(response.success) {
//window.location.href = "http://stackoverflow.com";
}else {
alert(response.data);
}
thisWrap.removeClass('loading');
},
error: function( jqXHR, textStatus, errorThrown ){
alert("Lỗi không xác định. Vui lòng thử lại sau");
thisWrap.removeClass('loading');
}
})
return false;
});
////////////////////////////////////////////////Function
$result['type'] = "success";
$result['post'] = $list_post;
wp_reset_query(); wp_reset_postdata();
echo json_encode($result);
//js var formvalue = form.serialize();
parse_str($_POST['data'], $params);
window.location.href = response.data;
location.reload();
API
add_action( 'wp_ajax_edmunds_api', 'edmunds_api_func' );
add_action( 'wp_ajax_nopriv_edmunds_api', 'edmunds_api_func' );
function edmunds_api_func() {
if(false == ($edmunds_data = get_transient('edmunds_data'))){
$request = wp_remote_get(
'https://api.edmunds.com/api/vehicle/v2/makes?fmt=json&api_key=xxxxxx',
array(
'timeout' => 120,
'httpversion' => '1.1'
)
);
$edmunds_data = wp_remote_retrieve_body( $request );
if ('OK' == wp_remote_retrieve_response_message( $request )OR 200 == wp_remote_retrieve_response_code( $request )){
set_transient('edmunds_data', $edmunds_data,'60*60*24*7');
}
}
//print_r($edmunds_data);
echo json_encode($edmunds_data);
die();
}
var xhr_compare = null;
function search_to_compare(form){
var keyword = $('.input_search_compare', form).val();
var prod = $('.prod_compare', form).val();
var thisWrap = $(form).closest('.search_to_compare');
var resultWrap = $('.search_compare_result', thisWrap);
if(xhr_compare) {
xhr_compare.abort();
}
if($.trim(keyword).length >= 2){
xhr_compare = $.ajax({
type : "post",
dataType : "json",
url : devvn_array.admin_ajax,
data : {
action: "devvn_compare",
keyword: keyword,
prod: prod
},
context: this,
beforeSend: function(){
thisWrap.addClass('compare_loading');
},
success: function(response) {
thisWrap.removeClass('compare_load_noresult');
resultWrap.show();
if(response.success) {
resultWrap.html(response.data);
}else {
thisWrap.addClass('compare_load_noresult');
resultWrap.html(response.data);
}
thisWrap.removeClass('compare_loading');
xhr_compare = null;
},
error: function( jqXHR, textStatus, errorThrown ){
xhr_compare = null;
thisWrap.removeClass('compare_loading');
}
});
}else{
resultWrap.html('').hide();
}
return false;
}