I am trying to create my own first plugin but I am coding novice so please guide me to the right direction.

This plugin is to set the post expiry date based on the user's role. Now I am stuck at the saving data phase.

Since I don't want to create a new database table for the inputs. I using the settings api to do the work and saving the data in wp_options table.

In my plugin setting page, I want the user able to add/edit/delete the rules. Each rule is defined by the user, with multiple inputs.

To add a rule, the user to select the role, post_type, expired_action, and day_limits. I want these inputs to be stored in an array, how can I do that?

For example :

array(1) {
role           => "" // get from the form input
post_type      => "" // get from the form input
expired_action => "" // get from the form input
day_limti      => "" // get from the form input 

array(2) {
    role           => "" 
post_type      => "" 
expired_action => ""  
day_limti      => "" 

array(3) {
role           => "" 
post_type      => "" 
expired_action => "" 
day_limti      => ""    

How do I use the input name my html form so that they can saved in an array every time a user submit a rule?

This is my html form in plugin option page for adding the rule:

<form action="options.php" id="apext_form" method="post" name="author_limit">
              <?php settings_fields('apext_Options'); ?>

    <table class="form-table">
        <tr valign="top">
        <label for="apext_user_role"><?php _e('User Role', 'apext') ?></label>
        <select name="role" id="apext_role_id">
           <?php global $wp_roles; if ( !isset( $wp_roles ) ) $wp_roles = new WP_Roles();
            foreach ( $wp_roles->role_names as $role => $name ) { ?>
            <option value="<?php echo $name; ?>"><?php echo $name; ?></option>
                    <?php }

        <label for="apext_post_type"><?php _e('Post Type', 'apext') ?></label>  
        <select name="apext_pt" id="apext_pt_id">
          <?php $post_types=get_post_types('','names'); 
               foreach ($post_types as $post_type ) {
            <option value="<?php echo $post_type;?>"><?php echo $post_type;?></option>
            <?php }


        <td><label for="Expired Action"><?php _e('Expired Action'); ?></label>
         <select name="remove_to" id="re_to"><option value =''></option>
                                             <option value="pending">pending</option>
                             <option value="draft">draft</option>
                             <option value="trash">trash</option>
                             <option value="delete">delete</option>

         <label for="apext_post_limit"><?php _e('Days Limit', 'apext') ?></label>   
                 <input type="text" id="apext_lim" name="apext_lim" id="lim" size="4" value="<?php echo get_option('apext_lim');?>"/><br>

                 <td><input class="button-primary" type="submit" name="Submit" value="<?php _e("Submit Rule", 'apext') ?>" /></td>

I know the form is not working, since it has no reference name from register_settings. It just help you to understand what I am trying to do.

Again, my question: how can I saved those input values in an array?

2 Answers 2


This is the way I like to handle collection of form inputs. Detailed explanation is in the comments.

if( isset( $_POST ) ) {

    // Create an empty array. This is the one we'll eventually store.
    $arr_store_me = array();

    // Create a "whitelist" of posted values (field names) you'd like in your array.
    // The $_POST array may contain all kinds of junk you don't want to store in
    // your option, so this helps sort that out.
    // Note that these should be the names of the fields in your form.
    $arr_inputs = array('input_1', 'input_2', 'input_3', 'etc');

    // Loop through the $_POST array, and look for items that match our whitelist
    foreach( $_POST as $key => $value ) {

        // If this $_POST key is in our whitelist, add it to the arr_store_me array
        if( in_array( $key, $arr_inputs) )
            $arr_store_me[$key] = $value;


    // Now we have a final array we can store (or use however you want)!
    // Update option accepts arrays--no need
    // to do any other formatting here. The values will be automatically serialized.
    update_option('option_name', $arr_store_me)


This is just a general approach. You can improve this by creating an array of default values for the fields, and comparing it against your "arr_store_me" array using wp_parse_args(). If you combine this with an extract($arr_store_me), it becomes a great way to prefill form fields while avoiding warnings for unset vars.

  • Is there any option such as update_option('option_name[test1]',$arr_store_me) ?
    – melvin
    Feb 8, 2018 at 9:06

If you want to save an array in option settings.

<div id="filter_sort">
    <?php $sorted_entites = get_option('filter_sort');
    foreach ($sorted_entites as $entity) { ?>
           <input type="hidden" class="fileter" name="filter_sort[]" value=<?php echo $entity; ?> />
           <?php echo get_the_title($entity); ?>
    <?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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