Created
September 21, 2015 15:05
-
-
Save zanematthew/e581e72627980d113d8b to your computer and use it in GitHub Desktop.
ZM ALR 2.0.0 -- Adding a new field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adding a new field to the register form | |
* | |
* These are the steps needed to add a new field in ZM ALR (2.0.0+) | |
* They are as follows: | |
* 1. Add your field to the array of fields | |
* 2. Check the validation of your new field | |
* 3. Do something with the value AFTER successful registration | |
* | |
*/ | |
/** | |
* Step 1 -- Add your field | |
* | |
* Your field should can contain the following params: | |
* $new_field['extra'] Any extra attributes, i.e., 'data-age="" data-key=""' | |
* $new_field['required'] If field should have the HTML "required" attribute | |
* $new_field['size'] Input field sze | |
* $new_field['name'] HTML name | |
* $new_field['id'] The HTML element ID | |
* $new_field['placeholder'] The HTML5 placeholder text | |
* $new_field['type'] Current types are: text, password, email, checkbox, html, submit | |
* $new_field['html'] If 'type' is set to HTML you can use 'html' to add any HTML you want | |
* $new_field['value'] The initial value | |
* | |
* @param (array) $fields The array of existing fields | |
* @return (array) $fields An array containing existing fields and the new field | |
* | |
*/ | |
function my_new_field( $fields ){ | |
$new_field['my_new_field_id'] = array( | |
'title' => 'Age', | |
'type' => 'text' | |
); | |
# c/o http://stackoverflow.com/a/1783125 | |
# Insert at offset 2 | |
$offset = 2; | |
$fields = array_slice( $fields, 0, $offset, true ) + | |
$new_field + | |
array_slice( $fields, $offset, NULL, true ); | |
return $fields; | |
} | |
add_filter( 'zm_alr_register_form_fields', 'my_new_field' ); | |
/** | |
* Step 2 -- Validate your field and handle the status | |
* | |
* If the validation fails the status should return an array containing the following: | |
* $status['description'] The message displayed to the user | |
* $status['cssClass'] The CSS class, set this to: 'noon' | |
* $status['code'] The code, set this to: 'error' | |
* | |
* @param (array) $status The status code | |
* @param (array) $values The values found in the form, i.e., $_POST | |
* @return (mixed) $status The status, if valid, return true | |
*/ | |
function my_new_field_status( $status, $values ){ | |
$age = trim( $values['my_new_field_id'] ); | |
if ( empty( $age ) ){ | |
$status = array( | |
'description' => 'Age is required', | |
'cssClass' => 'noon', | |
'code' => 'error' | |
); | |
} | |
return $status; | |
} | |
add_filter( 'zm_alr_register_submit_pre_status_error', 'my_new_field_status', 15, 2 ); | |
/** | |
* Step 3 -- Do something with the new value | |
* | |
* If the status returns true then the new user will be added to WordPress. You can | |
* retrieve your value from $_POST, and sanitize/validate, then do something with the value. | |
* | |
* @param (int) $user_id The user id | |
* @return (void) | |
*/ | |
function my_new_field_submit( $user_id ){ | |
// echo "ID: {$user_id}\n"; | |
// echo "params: "; | |
// print_r( $_POST ); | |
// die(); | |
} | |
add_filter( 'zm_alr_register_after_successfull_registration', 'my_new_field_submit' ); |
Where should I add this file/code?
Could you provide an example (say, for a phone number)?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doesn't work for me, every time i try to add something to: function my_new_field_submit( $user_id ) {}
the register process get stuck, and doesn't redirect to where it should.