php [会员2] - 自定义注册字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [会员2] - 自定义注册字段相关的知识,希望对你有一定的参考价值。

<?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;
        private static $_fields = array();
        private static $_fields_keys = array();
        
        
        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 );

            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 );
            
        }


        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 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 );
    
}

以上是关于php [会员2] - 自定义注册字段的主要内容,如果未能解决你的问题,请参考以下文章

php RCP Ultimate会员自定义暂停消息

如何更改woocommerce自定义用户注册字段中错误显示的顺序

请教高手,关于会员空间的问题 帝国cms

PHP-会员登录与注册例子解析-学习笔记

ecshop会员注册如何去除手机验证码

php如何查找会员无限分类的所有上级和所有下级