Extending the Reserve Form
1. In order to accomplish this job you are going to first have to setup your own reserve-form.php file via the template override system. This is important because if you ever do an update with us, your form will not get wiped out. So, you will want to copy that from the plugins directory and then setup the override system as explained here:
2. So, now that you have copied over the file to a location that looks like:
/wp-content/themes/your-theme-name/wpinventory/views/reserve-form.php
We can start to modify it.
3. For this tutorial, we will setup a check for the logged in user’s ID. You want to paste this block of code inside the reserve-form.php file above the submit button some place. You can put it wherever you want, just make sure you insert it between areas of PHP:
<?php if ($display_user) { // Use WordPress Built in functionality to find the logged in user if (!is_user_logged_in()) { $user = 'Unknown User'; } else { $user = get_current_user_id(); } $required = ($display_user == 2) ? ' required' : ''; ?> <div class="user"<?php echo $required; ?> style="display: none;"> <label><?php echo $user_label; ?><?php if ($required) { echo '<span class="req">*</span>'; } ?></label> <input type="hidden" name="wpinventory_reserve_user" value="<?php echo $user; ?>"<?php echo $required; ?> /> </div> <?php } ?>
Please notice the inline style that is “display: none;”. You can do this to hide this block from the form. It is an input we are going to take automatically without the user filling out anything at all. And, to that point, the input is also type=”hidden”. If the user is not logged in, it assigns the value “Unknown User”. You can get creative and do your own checks for different conditions, etc., but for this tutorial we have done a basic one for a default value.
add_action('wpim_reserve_config', 'my_reserve_form_customizations'); function my_reserve_form_customizations($args) { $args = wpinventory_reserve_add_field($args, 'user', 'optional', 'User ID', 'wpinventory_reserve_name'); return $args; }
What this is doing is passing the information along to the processor once the form field you just added in step three above has been added. You need the add_action and you need the function. What this function does is accepts five arguments:
4.1 –
$args
– you always have to have this and never need to change it. It is always
$args
4.2 – The next one is the name of the input. Ours is ‘user’
4.3 – The next one is if it is optional or required. I have elected to make it optional.
4.4 – The next one is the label. The text that will sit next to the input on the form. It is also the text that will be passed in the confirmation email.
4.5 – The last one is the position in the form. When you want to put the field in a certain position you need to know the name value of the field before it. Passing this to the fifth parameter of this function will tell it to put the new field directly above the one you gave it the name for.
5. To setup another field, you would just repeat the process by adding a new line like this:
$args = wpinventory_reserve_add_field($args, 'user', 'optional', 'User ID', 'wpinventory_reserve_name'); $args = wpinventory_reserve_add_field($args, 'project', 'required', 'Project', 'wpinventory_reserve_user');
So in this example, we added another field right under the ‘user’ called ‘project’ and gave it a label of “Project” and we want it to be required. We then have to go back over to the reserve form, and again add that field:
<?php if ($display_project) { $required = ($display_project == 2) ? ' required' : ''; ?> <div class="state"<?php echo $required; ?>> <label><?php echo $project_label; ?><?php if ($required) { echo '<span class="req">*</span>'; } ?></label> <input type="text" name="wpinventory_reserve_project" value="<?php echo $project; ?>"<?php echo $required; ?> /> </div> <?php } ?>
6. You can now navigate to your form and fill it out and submit it. You will receive an email that will have these details in it for you as well as whatever normal settings you have set through the dashboard.