Last active
September 2, 2021 22:41
-
-
Save wpmudev-sls/62350b236e2753c3a434d54beb50d8fa to your computer and use it in GitHub Desktop.
[Membership 2] - Custom registration fields
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 | |
/** | |
* 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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should add the file to the /wp-content/mu-plugins folder. Just create the folder with your FTP client.