While working on a WordPress plugin for fetching a certain number of RSS feeds, I have ran into a bit of a design problem.
The URLs for the feeds should be able to be configured by the user, which is working perfectly fine for a fixed number of feeds. The user can enter the URLs for these feeds on the appropiate Settings page, which obviously uses WordPress’ Settings API.
My goal, however, is to give the user the ability to enter an unknown number of feeds, along with labels and IDs to display and identify them. The problem lies in the way to represent this data. My desired result is to have a function that returns (something like) this:
$feed_categories = array(
array(
'id' => 'tools',
'label' => 'Tools',
'url' 'example.org/toolsfeed/feed.xml',
),
array(
'id' => 'questions',
'label' => 'Questions',
'url' => 'www.example.com/feeds/questions.xml',
),
// Etc
);
Now, I know I can store this kind of array using the WordPress Settings API I’m currently using, but I’m unsure…
- if that’s not what the Settings API was meant for, and
- how I would make an option page that permits “unlimited” adding of section of three key-value pairs, and how to parse that
Edit: the main reason I started questioning my current approach, is that the Settings (or Options) API has a simple way to generate forms for a fixed number of items, but not more. Secondly, and more importantly, I have not yet discovered a way to create a custom options.php
page, which would be needed in order to access the incoming POST request, after which I need to proceed to validate, format, and save the sent configuration data. So basically, the practical aspect of creating a form and handling the input seems quite limited to me when I’m using the Settings API. A capability to extend the options.php
‘s functionality would be great. (Maybe I’m overlooking a hook that does what I want?)
I have looked at the validate callback for Settings, but that would involve registering a setting which you don’t really want to save, just to access the corresponding $_POST
key.
The other option is this scenario is just making a table to store this information. However, since this probably is something most users will set and forget for a long time, I’m worried it could be overkill.
Do any of you know a good way to implement this in the Settings API without too much hacking, or does this really just call for a custom table?
I’d love to hear what you think about this, and why you think so.
easiest way to do this would be like you said, just store it in the database. This makes things nice and simple. It’s not much data, but it’s not necessarily “overkill” to do, you’re just using what is available to you.
If you really don’t want to use a database, a small “hack” you can do is to store the information using the options API. usually you would just store a key value like so:
However, if you want, you can store a whole array as a value. Using your example:
The only downside of doing this is, since you’re storing the whole array, when you update feed_categories, you need to
so you can’t do something like getting only the first part of the array, etc. You must work with the whole array everytime.