diff --git a/editor/js/graph.js b/editor/js/graph.js
index fe609201e98a99507ebaaddf3fa80204b87d4dcd..ef62c30b1f73c58dd83de8f17f6fa1c5f53cea9b 100644
--- a/editor/js/graph.js
+++ b/editor/js/graph.js
@@ -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",
diff --git a/knowledge-space.php b/knowledge-space.php
index b9363db8f548d138491ca44a22357f87a2088b64..38c6b27ea36db92aecd8a2cde7edbe782ab2a3d1 100644
--- a/knowledge-space.php
+++ b/knowledge-space.php
@@ -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');