Where should my plugin store user specific data?

I am writing a multisite-compatible plugin. My plugin will store data about each user for the current site that they are on. For example,

*billsmith, cows.example.com,

** heffers $35

** cows $17

*billsmith, hens.example.com

** eggs $5

** hens $45

What is best practice for storing this data?

  • Mrinal
    • Site Builder, Child of Zeus

    Hi James,

    If you’re creating a plugin for Multsite then it’s better to place it at `wp-content/mu-plugins’ folder. If the folder exist already then simply paste you plugin file/folder there & activate via WP Dashboard, if not exist then create a folder yourself & then paste your plugin.

    Hope this helps, Thanks!

  • PC
    • WPMU DEV Initiate

    Hello there James,

    Thanks for posting on the forums.

    That question will be best answered by one of my colleagues from the Second Level Support as it includes both the code and the db and the best practices can be shared by devs :slight_smile:

    One of them should be around soon.

    Cheers, PC

  • Saurabh
    • New Recruit

    Hi James,

    My plugin will store data about each user for the current site that they are on. For example,

    *billsmith, cows.example.com,

    ** heffers $35

    ** cows $17

    *billsmith, hens.example.com

    ** eggs $5

    ** hens $45

    Are you going to access Bill’s data across all the blogs, together?

    Or, are you going to access Bill and Jane and everyone else’s data on cow.example.com, together?

    Which of the above is going to be more frequent?

    If it is the first, store everything in user meta. If the second, store everything in blog option.

    I hope, it is self-evident. If it is not, I’ll be glad to explain in more detail.

    Best

    Saurabh

  • James Bayley on WPMU
    • Site Builder, Child of Zeus

    Dear Saurabh,

    Thank you for your advice. I now understand the options available to me.

    Each datum has two dimensions Blog, User and a Key and Value. One consideration is whether the data would have meaning if the blog was moved outside the multisite network. This might drive me to store it in the blog options, conversely when used in a BuddyPress network it should naturally be stored in the user meta.

    A third option might be to create a custom table for the data. Is this good practice? If so, is there a preferred way to do it in a multi-site environment?

    James

  • Saurabh
    • New Recruit

    Hi James,

    There is no preferred way to do this. It boils down to what works best for your use.

    Personally, when I am not sure of where to store such data, I choose whichever makes more sense, at the moment. It isn’t very complicated to switch to the other option, at a later stage. More often that not, clarity is achieved within a few development cycles.

    A fourth way to this, is to use both the methods. Redundancy is, in fact, considered a good thing. CRUD functions could be built to access data both ways. If one is unavailable, the system will access the other and also create the missing one. Sooner or later, you’d figure out which one to discard and which one to keep.

    Custom tables are absolutely fine. The only reason why one may not use them is when importing and exporting WordPress data from one site to another using the default tool. If we confine ourselves to WordPress’s default data structures, we don’t have to do anything. WordPress will export and import the data, very well.

    I hope that clarifies things and helps you make a informed decision.

    Best

    Saurabh

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.