admin管理员组

文章数量:1391987

I have created an option array that stores a series of options, along with their values. The problem is that I want to ACCUMULATE these options and store them in the options database. Meaning I would be able to add multiple records to the database, instead of only updating a single option.

For example, I need to store testimonials that will allow the user to keep the name/testimonial for several different people (i.e. Brad's testimonial, Carrie's Testimonial, etc), so the option would need the ability to add multiple records.

Hopefully this is enough description. Here's my current options code:

add_option( 'testimonials_settings', $testimonials_options );   

  $testimonials_options = array (
'testimonials_active' => '0',
'testimonials_name' => '',
'testimonials_website_name' => '',
'testimonials_website_url' => '',
'testimonials_quote' => ''
);

if(isset($_POST['testimonials_update_options'])) {
   $options = get_option('testimonials_settings'); 
   $options['testimonials_active'] = $_POST['testimonials_active'];
   $options['testimonials_name'] = $_POST['testimonials_name'];
   $options['testimonials_website_name'] = $_POST['testimonials_website_name'];
   $options['testimonials_website_url'] = $_POST['testimonials_website_url'];
   $options['testimonials_quote'] = $_POST['testimonials_quote'];
update_option('testimonials_settings', $options);
}

I have created an option array that stores a series of options, along with their values. The problem is that I want to ACCUMULATE these options and store them in the options database. Meaning I would be able to add multiple records to the database, instead of only updating a single option.

For example, I need to store testimonials that will allow the user to keep the name/testimonial for several different people (i.e. Brad's testimonial, Carrie's Testimonial, etc), so the option would need the ability to add multiple records.

Hopefully this is enough description. Here's my current options code:

add_option( 'testimonials_settings', $testimonials_options );   

  $testimonials_options = array (
'testimonials_active' => '0',
'testimonials_name' => '',
'testimonials_website_name' => '',
'testimonials_website_url' => '',
'testimonials_quote' => ''
);

if(isset($_POST['testimonials_update_options'])) {
   $options = get_option('testimonials_settings'); 
   $options['testimonials_active'] = $_POST['testimonials_active'];
   $options['testimonials_name'] = $_POST['testimonials_name'];
   $options['testimonials_website_name'] = $_POST['testimonials_website_name'];
   $options['testimonials_website_url'] = $_POST['testimonials_website_url'];
   $options['testimonials_quote'] = $_POST['testimonials_quote'];
update_option('testimonials_settings', $options);
}
Share Improve this question asked Dec 22, 2012 at 15:28 Rob MyrickRob Myrick 3054 silver badges18 bronze badges 4
  • What do you mean by "user"? The blog owner/administrator or ordinary users? – s_ha_dum Commented Dec 22, 2012 at 15:40
  • Hi s_ha_dum, the administrator will be the end user. – Rob Myrick Commented Dec 22, 2012 at 16:12
  • 1 If your going to have a testimonial it kind of sounds like you could use custom post types for each testimonial and have the cpt linked to a parent, and then just save those options as meta data of those cpts or 'testimonials'. – Vigs Commented Jul 18, 2013 at 2:23
  • 1 This is terrible data storage, as soon as you have any kind of record or 'entity' to store data against, you should consider a custom post type. Multiplexing records and options together in the options table is an awful way of doing things – Tom J Nowell Commented Aug 17, 2013 at 23:11
Add a comment  | 

3 Answers 3

Reset to default 1

The problem is that I want to ACCUMULATE these options and store them in the options database. Meaning I would be able to add multiple records to the database, instead of only updating a single option.

For example, I need to store testimonials that will allow the user to keep the name/testimonial for several different people (i.e. Brad's testimonial, Carrie's Testimonial, etc), so the option would need the ability to add multiple records.

What you're describing is not an option, but rather a Custom Post Type.

So, don't try to force custom content square peg into an option round hole. Instead, register a testimonial Custom Post Type.

The simplest solution is to change the settings key name.

update_option('testimonials_settings_brad', $options);
update_option('testimonials_settings_carrie', $options);

To retrieve them you'd need to know the individual key names or write your own SQL. Last I checked you couldn't pull these keys by wildcard like testimonials_settings_*. This will be an issue if you are trying to store different keys with what is presumably variable key names, like those based on people's names. You could store another option with the key name and use that to construct your other key names.

$names = array('brad','carrie');
update_option('testimonial_key_names',$names);

Then when you need your testimonials

$names = get_option('testimonial_key_names',array());
$testinomials = array();
foreach ($names as $n) {
    $testimonials[] = get_option('testimonials_settings_'.$n);
}

Of course, lots of testimonials means lots of queries so it isn't all that efficient.

You just build your array differently and store one value.

$options['brad']['testimonials_active'] = $_POST['testimonials_active'];
$options['brad']['testimonials_name'] = $_POST['testimonials_name'];
// and so on
$options['carrie']['testimonials_active'] = $_POST['testimonials_active'];
$options['carrie']['testimonials_name'] = $_POST['testimonials_name'];
// and so on
// then 
update_option('testimonials_settings', $options);

That solves several problems already mentioned but you could be storing a tremendous amount of information in the one spot, but that is a longtext column which is pretty big-- about 4GB I believe. I'd expect performance problems if it got that big though-- think memory error.

Those are the possibilities I can think of using the options API. Hopefully I haven't misread your problem.

I am wondering if you wouldn't be better off with a Custom Post Type for this. You have a complicated enough project that I'm leaning toward thinking that is the best option. You can register it in such a way that there is no front end post listing if you don't want that. You can even register it without the backend interface. See the link for parameters.

Looks like you are looking for Repeatable Options. I only have examples of Repeatable Fields. Hopefully it will be enough for you to get the idea and build your Options.
It should be a matter of translating the logic of MetaBoxes/CustomFields for Options and replacing get/update -> *_post_meta by *_option.

  • Create more Meta Boxes as needed - WPSE Q&A

  • Repeatable Custom Fields in a Metabox - Gist

    /**
     * Repeatable Custom Fields in a Metabox
     * Author: Helen Hou-Sandi
     *
     * From a bespoke system, so currently not modular - will fix soon
     * Note that this particular metadata is saved as one multidimensional array (serialized)
     */
    
  • Repeatable Custom Fields in a Metabox - Another Gist example, no description given

本文标签: plugin developmentHow to store accumulate multiple option values in a single array using OptionsAPI