Saving data in a JSON file with WordPress AJAX -
so have wordpress ajax function retrieves mysql data json , logs it. want not directly data on page ajax function is, want save data json file instead, can use wider variety of purposes.
here ajax function:
$.get( ajax_url, data, function(data) { // ajax callback fill_json(data); }// end ajax callback );
the fill_json()
function echo json data in table wrote myself.
now here happens inside ajax hook:
$sql_search = $wpdb->get_results(" complicated mysql search here "); if(!empty($_server['http_x_requested_with']) && strtolower($_server['http_x_requested_with']) == 'xmlhttprequest') { $result = json_encode($result); echo $result; } else { header("location: ".$_server["http_referer"]); }
underneath echo $result;
in ajax hook tried following piece of code, don't know how can see if worked or not:
$json_path = "/var/www/vhosts/jtc.ae/httpdocs/pre/wp/wp-content/themes/amazing_japan_hp/new/search.json"; file_put_contents($json_path, $result);
my question:
is correct way save data json file, , how can data on main page then?
extra question: saving $result
json file conflict multiple users using ajax @ same time?
all did right .. please make sure steps below :
- pass write permission folder /new inside "amazing_japan_hp" (chmod -r 666 /new)
- to save data in file use file_put_contents($file, $current);
to data file use $current = file_get_contents($file);
for newly created files try use umask. provides required permission files writing content in
umask() sets php's umask mask & 0777 , returns old umask. when php being used server module, umask restored when each request finished.
Thanks for give us valuable information
ReplyDeleteContact WordPress Support
WordPress Support Service
Very Helpful. Thanks for such Information
ReplyDeleteWP Support
WP Tech Support