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 => ""
}
......etc
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">
<tbody>
<tr valign="top">
<td>
<label for="apext_user_role"><?php _e('User Role', 'apext') ?></label>
<select name="role" id="apext_role_id">
<option></option>
<?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 }
?>
</select><br>
</td>
<td>
<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 }
?>
</select><br/>
</td>
<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>
</td>
<td>
<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>
<td><input class="button-primary" type="submit" name="Submit" value="<?php _e("Submit Rule", 'apext') ?>" /></td>
</tr>
</tbody>
</table>
</form>
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?
This is the way I like to handle collection of form inputs. Detailed explanation is in the comments.
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.
If you want to save an array in option settings.