Using the wpmem_register_form_rows filter allows you a simple way to add custom fields and text to the WP-Members registration form using the wpmem_array_insert() API function.
wpmem_register_form_rows receives an array of the rows that will make up the form. Each array element represents a field/row in the form and each of these elements contains an array of the various pieces that will make up that row (along with some additional data that can be used for logical/comparison tests to know what type of field you are dealing with).
To add a separator line of text using wpmem_register_form_rows, the easiest approach is to use the wpmem_array_insert() API function to insert your heading row directly into the $rows array being filtered.
Think of it like this: Suppose you have an original group of cards numbered 1, 2, 3, 4, and 5. You want a blue card inserted between 3 and 4. wpmem_array_insert() will allow you to insert “blue” after “3”. Now your new group is what you wanted: 1, 2, 3, blue, 4, 5.
Defining Row Elements
The $rows array being filtered is actually made up of arrays for each row. Each row element is basically this:
$new_row['address_heading'] = array( 'order' => '', 'meta' => '', 'type' => '', 'value' => '', 'row_before' => '', 'label' => '<label class="text"></label>', 'field_before' => '<div class="div_text">', 'field' => '<h3>Please include your address information</h3>', 'field_after' => '</div>', 'row_after' => '' );
The first thing to note is that this is an array with a key. In my example, I’m calling this ‘address_heading’. You can call it whatever you want to call it as long as the following are true:
- The value must be unique (i.e. if you add multiple headings, you can’t use ‘address_heading’ twice; and make sure you’re not using something that matches an existing field meta key).
- Don’t use spaces, numbers, or special characters other than underscores. ‘address_heading’ is OK, ‘Address Heading 1’ wouldn’t be advised.
When you create your heading row, you’ll need to define all of the possible elements needed for a row even if some of them are empty values. Specifically, order, meta, type, and value keys are not going to be used, but the key must be defined. Most people do not use row wrappers, so if your configuration is the basic default, row_before and row_after will likely be empty as well.
That leaves label, field_before, field, and field_after. Depending on your form layout, you may not need to provide anything for label either. If you use a side-by-side layout (labels next to fields) then you might need an empty label to keep things laid out correctly. But if you use one of the “no float” stylesheets, then you probably can leave this empty as well. (The example includes it.)
field_before and field_after is the div wrapper for the field. My example puts in the HTML that would ordinarily be there for a text field. Again, depending on layout an actual value here may or may not be necessary.
Lastly, the field value. This is where you will put your heading HTML. In my example, I’ve put this in as an <h3> heading. Use what makes sense in your layout/theme/style.
An Example
This example code snippet uses the row we talked about above and adds it as a separator into the form. While you don’t have to be using the WP-Members default fields for this example to work, it might help in visualizing. It at least assumes that you have the default “last_name” field and that you want a heading after that to read: “Please include your address information.” What comes after that doesn’t matter in the sense of understanding how this works, but it does make better sense if you have address information after the “last_name” field.
add_filter( 'wpmem_register_form_rows', 'my_field_separator', 10, 2 ); function my_field_separator( $rows, $tag ) { /* * Define an array of row elements. * Unused elements can be empty ('') but * still need to be defined. */ $new_row['address_heading'] = array( 'order' => '', 'meta' => '', 'type' => '', 'value' => '', 'row_before' => '', 'label' => '<label class="text"></label>', 'field_before' => '<div class="div_text">', 'field' => '<h3>Please include your address information</h3>', 'field_after' => '</div>', 'row_after' => '' ); /* * Add your new row into the $rows array using * wpmem_array_insert( $array, $new, $key ) * - $array The array you are adding to. * - $new The new element being added. * - $key The key in $array you will add after. */ $rows = wpmem_array_insert( $rows, $new_row, 'last_name' ); // If additional rows are needed, repeat the process. // Return filtered $rows array. return $rows; }
Notes
This example is cut-and-paste ready, but it assumes you are adding a heading after the “last_name” field. Change the key ‘last_name’ in the wpmem_array_insert() call to the appropriate meta key (field option name) to customize.
Adding additional heading rows amounts to adding a new row array and using the wpmem_array_insert() function to insert it. Repeat that section of code as many times as needed before you return at the end.
Not sure what to do with this code?
You’re not a “coder” and don’t know what to do? Don’t worry! Code Snippets are the basic building blocks of WordPress customization, and once you know the basics, they are simple to use.
Here are some articles to get you started: