Skip to content
Snippets Groups Projects
Commit 8e0a7b00 authored by Matthias Konitzny's avatar Matthias Konitzny :fire:
Browse files

Working ajax demo.

parent 0552d308
No related branches found
No related tags found
No related merge requests found
......@@ -47,15 +47,18 @@ export class Graph extends ManagedData {
this.calculateLinkTypes();
this.onChangeCallbacks = [];
let payload = {
action: "update_space",
name: "space",
payload: data,
};
jQuery.ajax({
type: "POST",
url: ajax_object.ajax_url,
// The key needs to match your method's input parameter (case-sensitive).
data: {
action: "update_space",
whatever: data,
},
//contentType: "application/json; charset=utf-8",
data: payload,
// contentType: "application/json; charset=utf-8",
// contentType: false,
// processData: false,
// dataType: "json",
......
......@@ -84,16 +84,25 @@ function get_space_id_from_atts($atts) {
}
}
add_action("wp_ajax_update_space", "update_space");
add_action("wp_ajax_nopriv_update_space", 'update_space' );
add_action("wp_ajax_update_space", "update_space"); // Fires only for logged-in-users
//add_action("wp_ajax_nopriv_update_space", 'update_space' ); // Fires for everyone
function update_space() {
global $wpdb;
//$arr_img_ext = array('image/png', 'image/jpeg', 'image/jpg', 'image/gif');
//$upload = wp_upload_bits($_FILES['file']['name'], null, file_get_contents($_FILES['file']['tmp_name']));
//echo "Hello";
echo $_POST["whatever"];
wp_die();
// Check user capabilities
if (current_user_can("edit_posts")) {
$plugin_dir = plugin_dir_path(__FILE__);
// Use json encoding.
$filename = $plugin_dir . $_POST["name"] . ".json";
$result = file_put_contents($filename, json_encode($_POST["payload"]));
//echo print_r($_POST);
echo "Saved file at ";
echo $filename;
echo $result;
wp_die();
} else {
echo "Insufficient permissions!";
}
}
add_action('wp_enqueue_scripts', 'kg_load_css');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment