Hello,
you don’t need a new database table to save your plugin data in wordpress,
What you need:
First:
user_meta it is a database table that saves data from user, so if you need to save some data in your plugin you can do something like this:
Imagine that I want to save hourses, oranges and apples for my user, then I create a class or array in my plugin to save this data, something like this:
class user_data{
public $hourses;
public $oranges;
public $apples;
public function __construct($oranges,$hourses,$apples){
$this->apples = apples;
$this->hourses = $hourses;
$this->oranges = $oranges;
}
}
When the user fills the form and say it has 3 horses, 2 apples and 7 oranges, and submit this information you will get this information in the $_POST.
Then in the server side when you get the post form, you will do:
$oranges = $_POST[‘oranges’];
$apples = $_POST[‘apples’];
$hourses = $_POST[‘hourses’];
$data = new user_data($oranges,$hourses,$apples);
//sample of user id
$user_id = 1;
you will have the current user id, then you will save the data for this user like:
add_user_meta( $user_id, ‘_my_plugin_data’, serialize($data));
With this you will save your user information in user meta with the key _my_plugin_data
to get the data and restore it, you only need to do:
$saved_data = unserialize(get_user_metadata($user_id,’_my_plugin_data’));
In the $saved_data you will have an object of user_data then you can access this data like:
$oranges = $saved_data->oranges;
$apples = $saved_data->apples;
$hourses = $saved_data->hourses;
This it is the right way to save the data for your users, you can do it using an array it will works, but to access the data will be different:
$oranges = $saved_data[‘oranges’];
$apples = $saved_data[‘apples’];
$hourses = $saved_data[‘hourses’];
best regards