• Hi, I’m using a plugin that has some user fields that I would like to be entered into the database when a new user joins my site. Currently each time a user joins, the data about the user is saved in the plugin but not in the wordpress database.

    Can someone tell me how to capture the data from any plugin and save it to the database? Perhaps point me to a tutorial on the subject?

    Thanks for any ideas that may point me in the right direction.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    You should save this data in usermeta, in this table you can save the information your plugin needs.
    To access the data you will need to find the row using the metakey and user_id

    regards

    Thread Starter RedChill

    (@redchill)

    Not quite sure I understand. I see user_id field and umeta_id field but how does knowing these id’s help me to grab the data that’s being entered into a field of a plugin and put it in my own table?

    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How can I save data from my plugin to the database?’ is closed to new replies.