Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

If I am not mistaken, the settings api only storing one single data array in wp_options table. You can only change the field data in the array.

What if I want to add multiple array in the same table with same array structure but different value.

What I mean is :

$data = array(
    '1' = array(
        'cpt'=> 'cpt1',
        'date'=> 'date1',
        'role'=> 'subscriber'            
    ),
    '2' = array(
        'cpt'=> 'cpt2',
        'date'=> 'date2',
        'role'=> 'eiditor'       
    ).....etc
 );

and I can add new array, edit the field inside either one of the array,or delete unwanted array.

What I found on goolge are only tutorial on how to store the data in single array, but I did see some plugin done this, but just couldn't understand how they done it with my current of programming level.

I don't want to create another custom table,since the data that I want to store is not so much. I only store some setting (user,post-type,post date) for further use.

Any guides or advice will be appreciated.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

I put this in the form so it knows its editting:

<form method="post" action="options.php">

     <?php settings_fields('venues_section'); ?>
     <input type="hidden" name="editmode" value="<?php echo $editmode; ?>" />
     <input type="hidden" name="venue_id" value="<?php echo $venue_id; ?>" />
     <?php submit_button(); ?>

</form>

The receiving validation function can then handle that by doing something like this:

function venues_validation_callback($input){

    $venues = get_option('venues'); // Get the current options from the db (Edit 
                                    // this and return the full modified version)
    $editmode = $_POST['editmode'];  // add, edit or delete     
    $venue_id = $_POST['venue_id'];             

    if($editmode == 'add'){

        // Do Add Logic

        return $venues;

    elseif($editmode == 'edit') {

        // Do Edit Logic

        return $venues;

    }
    elseif($editmode == 'delete'){

        // Do Delete Logic

        return $venues;

    }

    return $input; // only triggered if none of the above are called
}

That's just to show its possible. There's quite a lot of other ins and outs to it as well its a pretty custom solution. I've got it working but its taken a week and given me a few headaches doing it I'll be honest. But there you go that's about as much as I can do. Hopefully there's a plugin that could do it I hadn't found. It'd be nice to make it if it hasn't already if i had time.

share|improve this answer
    
This seem working. Thank you very much. –  dev-jim Aug 22 '12 at 14:35
    
danbrown,I just got the time to revisit this issue (I was busy with something else). I can tell what your codes are trying to do, but now my major problem is not able to figure out how to write the action logic codes. Can you show me how you write the Add,Edit,Delete logic codes? Thanks –  dev-jim Sep 14 '12 at 18:15

I just found a way of doing what I think you're after... otto just helped me out with it here.

"Look at your validation function (the one referenced by your register_setting call). Whatever it returns replaces the entire setting in the database. So if it's not getting the setting and then altering it and returning the entire setting, you'll lose the data that you didn't have it return."

So you can do all the manipulating of the array there...

share|improve this answer
    
From the link, I can see how to manipulating the input data for saving, but what if we want to delete some of the records? –  dev-jim Aug 19 '12 at 17:35
    
If I understand you right you can use the validate function to do deleting as well. Could you give an example? It is multiple arrays you want in the same options field? –  danbrown Aug 20 '12 at 8:37
    
yes, it suppose are multiple arrays. Each array represent a rule (eg,user,gender,age). So if I have many user, then I will have many rules. What I want is able to add, delete, update and read each of the array(rule). –  dev-jim Aug 20 '12 at 10:40
    
You can definately do it in the validate callback, I've just set it up - add, edit and delete. You need to let the validate function know whether it should add or edit or delete. I did it by putting an extra hidden field in the forms, I'll put it in an answer... –  danbrown Aug 22 '12 at 13:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.