I am developing plugin right now, and I have one questions about best practices and conventions.

What I need ?

My plugin is going to store some predefined object, list of objects (or just arrays/key-value pairs) and it will be possible to add new object and fill its fields.
For example my object will have following content

{
  "id": 123,
  "url": "http://google.com/",
  "enabled" : true,
  "name": "hello_world",
  "api_key" : "api_key"
}

Simple JSON object.

And on Plugin Admin configuration page it will be possible to add, edit or remove such objects.

What is my question ?

What is the best way of storing such data. I have installed a lot of different plugins in order to see how does custom data from settings is stored. There some options I have seen

  1. Using Settings API provided by WordPress. Even UI is handled by wordpress, you can just call the function and it will create proper input field and than save all settings into options table. All checks and security is handled by wordpress as well. But is it possible to create dynamic admin page where you can add new items ?

  2. Using old Options API is also stored in the options table, but gives more freedom to developer to handle all validation.

  3. Creating new database table and save data in it.

I don’t think I am going to use third method.

Please suggest better way to do this or maybe you know the plugin already implemented such functionality in a right way. I would grateful for any help.

3 s
3

It depends on how you are going to use the stored data.

If you want to run complex queries against the values, use a custom table with indexes optimized for those queries.

If you will always just fetch all the values for a given object, create a non-public custom post type, and store the data as post meta.

Do not store the data in a serialized string, like the options API does it. This is a nightmare when you want to change it per command line SQL, because the serialized format is PHP specific.

The “Settings API” imposes some very outdated markup, and the code for the registration and storage is rather counter-intuitive. I wouldn’t recommend to use it.

Leave a Reply

Your email address will not be published. Required fields are marked *