添加自定义配置文件字段


Add custom profile fields

我正在尝试为我正在工作的WordPress网站添加2个电话号码的自定义用户配置文件字段。我已经尝试了互联网上的一切,所以我可以在这里联系。

我还需要将字段添加到注册页面,以与另一个插件集成。我已经是WordPress的本地插件了,所以这只是添加注册上的字段的问题。

我已经能够在wp-admin编辑配置文件页面中添加字段到编辑配置文件页。但我无法将它们添加到注册页面。

这是我迄今为止工作过的代码:

$extra_fields =  array( 
                                    array( 'company_phone', __('Company Phone', 'rc_cucm'), true ),
                                    array( 'personal_phone', __('Personal Phone', 'rc_cucm'), true ),
                                    );
                                    // Use the user_contactmethods to add new fields
                add_filter( 'user_contactmethods', 'rc_add_user_contactmethods' );
                /**
                 * Add custom users custom contact methods
                 *
                 * @access      public
                 * @since       1.0 
                 * @return      void
                */
                function rc_add_user_contactmethods( $user_contactmethods ) {
                    // Get fields
                    global $extra_fields;
                    // Display each fields
                    foreach( $extra_fields as $field ) {
                        if ( !isset( $contactmethods[ $field[0] ] ) )
                            $user_contactmethods[ $field[0] ] = $field[1];
                    }
                    // Returns the contact methods
                    return $user_contactmethods;
                }

当我为注册页面添加这一部分时,我会得到错误"分析错误:语法错误,

中出现意外的T_STRING/home/content/e/endoredges/html/associates/wp-content/plugins/自定义用户联系方法/rc-custom-user-contact-methods.php在线44"

//This is where the error occur when I try to add fields to the registration page
        // Add our fields to the registration process
        2
        add_action( 'register_form', 'rc_register_form_display_extra_fields' );
        3
        add_action( 'user_register', 'rc_user_register_save_extra_fields', 100 );
        /**
         * Show custom fields on registration page
         *
         * Show custom fields on registration if field third parameter is set to true
         *
         * @access      public
         * @since       1.0 
         * @return      void
        */
        function rc_register_form_display_extra_fields() {
            // Get fields
            global $extra_fields;
            // Display each field if 3th parameter set to "true"
            foreach( $extra_fields as $field ) {
                if( $field[2] == true ) { 
                if( isset( $_POST[ $field[0] ] ) ) { $field_value = $_POST[ $field[0] ]; } else { $field_value = ''; }
                ?>
                <p>
                    <label for="<?php echo $field[0]; ?>"><?php echo $field[1]; ?><br />
                    <input type="text" name="<?php echo $field[0]; ?>" id="<?php echo $field[0]; ?>" class="input" value="<?php echo $field_value; ?>" size="20" /></label>
                    </label>
                </p>
                <?php
                } // endif
            } // end foreach
        }
        /**
         * Save field values
         *
         * @access      public
         * @since       1.0 
         * @return      void
        */
        function rc_user_register_save_extra_fields( $user_id, $password = '', $meta = array() )  {
            // Get fields
            global $extra_fields;
            $userdata       = array();
            $userdata['ID'] = $user_id;
            // Save each field
            foreach( $extra_fields as $field ) {
                if( $field[2] == true ) { 
                    $userdata[ $field[0] ] = $_POST[ $field[0] ];
                } // endif
            } // end foreach
            $new_user_id = wp_update_user( $userdata );
        }

对哪里出了问题的任何见解都会有所帮助。我觉得我离得很近,如果我错了,请告诉我。

查看概要文件生成器。您可以添加任意数量的额外自定义用户字段。

我真的会考虑看看一个名为Advanced Custom Fields的插件。让您将所有类型的自定义字段添加到页面、帖子、自定义帖子类型、附件,甚至用户!

高级自定义字段