Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Membership 2] - Custom registration fields
<?php
/**
* Plugin Name: [Membership 2] - Custom registration fields
* Plugin URI: https://premium.wpmudev.org/
* Description: Add custome fileds in Membership registration form
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_MS_Custom_Registration_Fields' ) ) {
class WPMUDEV_MS_Custom_Registration_Fields {
private static $_instance = null;
public static $_fields = array();
public static $_fields_keys = array();
public function form_fields(){
$fields = array(
'gender' => array(
'id' => 'gender',
'title' => __( 'Gender', 'membership2' ),
'field_options' => array( __( 'Male', 'membership2' ) , __( 'Female', 'membership2' ) ),
'type' => MS_Helper_Html::INPUT_TYPE_SELECT,
'value' => ''
),
'interests' => array(
'id' => 'interests',
'title' => __( 'Interests', 'membership2' ),
'field_options' => array( __( 'Technology', 'membership2' ), __( 'Philosophy', 'membership2' ) ),
'type' => MS_Helper_Html::INPUT_TYPE_RADIO,
'value' => ''
),
);
return $fields;
}
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_MS_Custom_Registration_Fields();
}
return self::$_instance;
}
private function __construct() {
self::$_fields = $this->form_fields();
self::$_fields_keys = array_keys( self::$_fields );
$this->set();
$this->overrides();
add_filter( 'ms_shortcode_register_form_fields', array( $this, 'inject_form_fields' ), 10, 2 );
add_filter( 'ms_view_profile_fields', array( $this, 'inject_form_fields' ), 10, 2 );
add_action( 'ms_model_member_create_new_user', array( $this, 'save_user' ), 10 );
add_action( 'ms_model_member_update_user', array( $this, 'save_user' ), 10 );
}
private function set() {
$member = MS_Model_Member::get_current_member();
foreach ( self::$_fields as $field_key => $_field_data ) {
$value = get_user_meta( $member->id, $field_key, true );
$field_data = (object) $_field_data;
if ( in_array( $field_data->type, array( MS_Helper_Html::INPUT_TYPE_SELECT, MS_Helper_Html::INPUT_TYPE_RADIO ) ) ) {
if ( is_numeric( $value ) && is_array( $field_data->field_options ) ) {
$value = $field_data->field_options[ $value ];
}
}
self::$_fields[ $field_key ][ 'value' ] = $value;
}
}
private function overrides() {
if ( ! function_exists( 'get_ms_ac_profile_info' ) ) {
function get_ms_ac_profile_info( $field ) {
if ( in_array( $field , WPMUDEV_MS_Custom_Registration_Fields::$_fields_keys ) ) {
return isset( WPMUDEV_MS_Custom_Registration_Fields::$_fields[ $field ]['value'] ) ? WPMUDEV_MS_Custom_Registration_Fields::$_fields[ $field ]['value'] : '';
}
return MS_Helper_Template::$ms_account['member']->$field;
}
}
if ( ! function_exists( 'get_ms_ac_profile_fields' ) ) {
function get_ms_ac_profile_fields() {
$member = MS_Model_Member::get_current_member();
$account_fields = MS_Helper_Template::$ms_account['fields']['personal_info'];
foreach ( WPMUDEV_MS_Custom_Registration_Fields::$_fields as $field_key => $_field_data ) {
$title = isset( WPMUDEV_MS_Custom_Registration_Fields::$_fields[ $field_key ]['title'] ) ? WPMUDEV_MS_Custom_Registration_Fields::$_fields[ $field_key ]['title'] : '';
$account_fields[ $field_key ] = $title;
}
return $account_fields;
}
}
}
public function inject_form_fields( $fields, $MS_View_Shortcode_RegisterUser ){
$submit_field = array();
if( isset( $fields[ 'submit' ] ) ){
$submit_field = $fields[ 'submit' ];
unset( $fields[ 'submit' ] );
}
$fields = array_merge( $fields, $this->set_fields_values( self::$_fields ) );
if( ! empty( $submit_field ) ){
$fields[ 'submit' ] = $submit_field;
}
return $fields;
}
public function set_fields_values( $fields ){
if( ! is_user_logged_in() ){
return $fields;
}
$user_meta = get_user_meta( get_current_user_id() );
foreach( $user_meta as $key => $meta ){
if( ! in_array( $key, self::$_fields_keys ) ){
continue;
}
$fields[ $key ][ 'value' ] = $meta[0];
}
return $fields;
}
public function save_user( $member ){
if( empty( $_POST ) ){
return;
}
foreach( $_POST as $field_key => $field_info ){
if( ! in_array( $field_key, self::$_fields_keys ) ){
continue;
}
update_user_meta(
$member->id,
$field_key,
$field_info
);
}
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_MS_Custom_Registration_Fields'] = WPMUDEV_MS_Custom_Registration_Fields::get_instance();
}, 10 );
}
@wpmudev-sls

This comment has been minimized.

Copy link
Owner Author

wpmudev-sls commented Oct 4, 2017

You will need to modify the fields in the form_fields() function ( starting at line 47 ).

For type you can use any available:

MS_Helper_Html::INPUT_TYPE_HIDDEN 		    = 'hidden';
MS_Helper_Html::INPUT_TYPE_TEXT_AREA 		= 'textarea';
MS_Helper_Html::INPUT_TYPE_SELECT 		    = 'select';
MS_Helper_Html::INPUT_TYPE_RADIO 			= 'radio';
MS_Helper_Html::INPUT_TYPE_SUBMIT 		    = 'submit';
MS_Helper_Html::INPUT_TYPE_BUTTON 		    = 'button';
MS_Helper_Html::INPUT_TYPE_CHECKBOX 		= 'checkbox';
MS_Helper_Html::INPUT_TYPE_IMAGE 			= 'image';
// Different input types
MS_Helper_Html::INPUT_TYPE_TEXT 			= 'text';
MS_Helper_Html::INPUT_TYPE_PASSWORD 		= 'password';
MS_Helper_Html::INPUT_TYPE_NUMBER 		    = 'number';
MS_Helper_Html::INPUT_TYPE_EMAIL 			= 'email';
MS_Helper_Html::INPUT_TYPE_URL 			    = 'url';
MS_Helper_Html::INPUT_TYPE_TIME 			= 'time';
MS_Helper_Html::INPUT_TYPE_SEARCH 		    = 'search';
MS_Helper_Html::INPUT_TYPE_FILE 			= 'file';

/* Constants for advanced HTML input elements. */
MS_Helper_Html::INPUT_TYPE_WP_EDITOR 		= 'wp_editor';
MS_Helper_Html::INPUT_TYPE_DATEPICKER 	    = 'datepicker';
MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER 	= 'radio_slider';
MS_Helper_Html::INPUT_TYPE_TAG_SELECT 	    = 'tag_select';
MS_Helper_Html::INPUT_TYPE_WP_PAGES 		= 'wp_pages';

/* Constants for default HTML elements. */
MS_Helper_Html::TYPE_HTML_LINK 			    = 'html_link';
MS_Helper_Html::TYPE_HTML_SEPARATOR 		= 'html_separator';
MS_Helper_Html::TYPE_HTML_TEXT 			    = 'html_text';
MS_Helper_Html::TYPE_HTML_TABLE 			= 'html_table';

When you need to use options ( like in a select, or radio ) you can use the field_options key and add an array with options as in the example in this snippet

@DigicalAU

This comment has been minimized.

Copy link

DigicalAU commented Jan 8, 2019

Where do you add this code after modifying the form fields?

Thanks

@alexvickers

This comment has been minimized.

Copy link

alexvickers commented Apr 1, 2019

You should add the file to the /wp-content/mu-plugins folder. Just create the folder with your FTP client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.