Creating Custom REST API in WordPress

To create custom APIs on my WordPress website, I have installed two plugins: “WP REST API v2” and “JWT Authentication for WP-API.” However, I am encountering difficulties as I cannot seem to write any data into the database despite my efforts.

The website is hosted on Auba.

Here my .php file :

<?php
/*
Plugin Name: Custom API
Description: Plugin per le API personalizzate per la tabella custom_table.
*/

// Esempio di impostazione di sql_mode
global $wpdb;
$wpdb->query("SET SESSION sql_mode = 'valore_desiderato';");

// Registra le routes API personalizzate
add_action('rest_api_init', 'register_custom_api_routes');

function register_custom_api_routes() {
    register_rest_route('custom/v1', '/data', array(
        'methods' => 'GET',
        'callback' => 'get_custom_data',
    ));

    register_rest_route('custom/v1', '/data', array(
        'methods' => 'POST',
        'callback' => 'create_custom_data',
        'permission_callback' => function () {
            return current_user_can('edit_posts'); // Solo gli utenti con il permesso di edit_posts possono creare nuovi dati
        },
    ));

    register_rest_route('custom/v1', '/data/(?P<id>d+)', array(
        'methods' => 'PUT',
        'callback' => 'update_custom_data',
        'permission_callback' => function () {
            return current_user_can('edit_posts'); // Solo gli utenti con il permesso di edit_posts possono aggiornare i dati
        },
    ));

    register_rest_route('custom/v1', '/data/(?P<id>d+)', array(
        'methods' => 'DELETE',
        'callback' => 'delete_custom_data',
        'permission_callback' => function () {
            return current_user_can('edit_posts'); // Solo gli utenti con il permesso di edit_posts possono eliminare i dati
        },
    ));
}

// Funzione per ottenere tutti i dati dalla tabella custom_table
function get_custom_data() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'custom_table';
    $query = "SELECT * FROM $table_name";
    $results = $wpdb->get_results($query, ARRAY_A);
    var_dump($results);
    return rest_ensure_response($results);
}

// Funzione per creare un nuovo dato nella tabella custom_table
function create_custom_data($request) {
    $data = $request->get_json_params();
    if (empty($data['nome'])) {
        return new WP_Error('empty_data', 'Il campo "nome" non può essere vuoto.', array('status' => 400));
    }

    global $wpdb;
    $table_name = $wpdb->prefix . 'custom_table';
    $wpdb->insert($table_name, array('nome' => $data['nome']), array('%s'));
    $id = $wpdb->insert_id;

    return rest_ensure_response(array('id' => $id, 'nome' => $data['nome']));
}

// Funzione per aggiornare un dato esistente nella tabella custom_table
function update_custom_data($request) {
    $data = $request->get_json_params();
    if (empty($data['nome'])) {
        return new WP_Error('empty_data', 'Il campo "nome" non può essere vuoto.', array('status' => 400));
    }

    $id = $request->get_param('id');
    global $wpdb;
    $table_name = $wpdb->prefix . 'custom_table';
    $wpdb->update($table_name, array('nome' => $data['nome']), array('id' => $id), array('%s'), array('%d'));

    return rest_ensure_response(array('id' => $id, 'nome' => $data['nome']));
}

// Funzione per eliminare un dato dalla tabella custom_table
function delete_custom_data($request) {
    $id = $request->get_param('id');
    global $wpdb;
    $table_name = $wpdb->prefix . 'custom_table';
    $wpdb->delete($table_name, array('id' => $id), array('%d'));

    return rest_ensure_response(array('message' => 'Dato eliminato con successo.'));
}

I get a 200 response for each API call, but in reality, I don’t see any data being saved in the database. As an administrator, I have all the necessary permissions for both reading and writing to the database, and the website functions without any issues, as I can save articles, images, etc.

What I’m doing wrong?