I have been trying to return a $wpdb->insert_id; but unfortunately, it is returning null;
That’s not true, you haven’t been returning $wpdb->insert_id
at all.
// insert
$toevoegen = $wpdb->insert(
...
);
$lastinsertid = $toevoegen->insert_id;
You’ve been returning $toevoegen->insert_id
, which is wrong. According to the docs $wpdb->insert
will return either false
or it will return the number of rows inserted:
This method returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1).
false->insert_id
is not valid and null
. Neither is 1->insert_id
.
Instead, return $wpdb->insert_id
instead.
https://developer.wordpress.org/reference/classes/wpdb/#insert-row