0
votes

I am trying to add 2 custom user profile fields for phone numbers to a WordPress site I am working on. I have tried everything on the internet at this point so I am reach out here.

I also need to add the fields to the registration page to integrate with another plugin I am already the registration is native to WordPress already so it's just a matter of adding of having the fields on the registration.

I have been able to add the fields to the edit profile page in the wp-admin edit-profile page. But I cannot add them to the registration page.

This is the code I have worked so far:

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

and when I add this part for the registration page I get the error "Parse error: syntax error,

unexpected T_STRING in /home/content/e/t/e/eternalreefs/html/affiliates/wp-content/plugins/custom-user-contact-methods/rc-custom-user-contact-methods.php on line 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 );
        }

Any insight into what is going wrong would be helpful I feel like I am so close please let me know if I am wrong.

2
Try to fix your indentation. - Cyclonecode
Can you elaborate on that? - user2977705
I meant that you should edit your question and improve you indentation so the code is easier read and understood. - Cyclonecode

2 Answers

1
votes

Have a look at Profile Builder. You can add as many extra custom user fields as you want.

0
votes

I would really consider taking a look at a plugin called Advanced Custom Fields. Let's you add all types of custom fields to Pages, Posts, Custom Post Types, Attachments, even Users!

Advanced Custom Fields