├── .gitignore ├── readme.txt ├── custom-multisite-signups-extras.php └── custom-multisite-signups.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === Custom Multisite Signups === 2 | Contributors: afragen 3 | Requires at least: 3.4 4 | Tested up to: 3.9.1 5 | Plugin URI: https://github.com/afragen/custom-multisite-signups 6 | GitHub Plugin URI: https://github.com/afragen/custom-multisite-signups 7 | Stable tag: master 8 | Version: 0.2.0 9 | License: GPLv2 or later 10 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 11 | 12 | Framework for adding custom registration info during WP Multisite registration. 13 | 14 | == Description == 15 | 16 | This plugin adds first name, last name and sets display name during registration for WP Multisite site. Plenty of hooks are included so additional registration info and additional custom fields may be added. This can be added using another plugin. The custom-multisite-signups-extra.php file that is included here as an example. 17 | 18 | == Installation == 19 | 20 | This section describes how to install the plugin and get it working. 21 | 22 | 1. Upload `custom-multisite-signups` to the `/wp-content/plugins/` directory 23 | 1. Activate the plugin through the 'Plugins' menu in WordPress 24 | 25 | == Frequently Asked Questions == 26 | 27 | 28 | == Changelog == 29 | 30 | = 1.0.0 = 31 | * Initial commit 32 | 33 | -------------------------------------------------------------------------------- /custom-multisite-signups-extras.php: -------------------------------------------------------------------------------- 1 | Extra Field'; 18 | $extra_fields[] = ''; 19 | 20 | $html .= implode( "\n", $extra_fields ); 21 | echo $html; 22 | } 23 | 24 | public function extra_fields_selectors( $selectors ) { 25 | $selectors .= ', .mu_register #extra_field1'; 26 | return $selectors; 27 | } 28 | 29 | public function extra_fields_css( $css ) { 30 | $css .= ' /* CSS comment */ '; 31 | return $css; 32 | } 33 | 34 | public function validate_user_signup( $result ) { 35 | if ( empty( $_POST['extra_field1'] ) ) { 36 | $result['errors']->add( 'last_name', __( 'You must include an extra field.' ) ); 37 | echo '

', $result['errors']->get_error_message('extra_field1'), '

'; 38 | } 39 | 40 | return $result; 41 | } 42 | 43 | public function cms_extra_signup_meta() { 44 | return array( 45 | 'extra_field1' => sanitize_text_field( $_POST['extra_field1'] ), 46 | ); 47 | } 48 | 49 | public function show_extra_profile_fields( $user ) { 50 | 51 | $html[] = ''; 52 | $html[] = ''; 53 | $html[] = '
'; 56 | $html[] = 'Please enter your Extra Field 1 data.'; 57 | $html[] = ''; 58 | $html = implode( " ", $html ); 59 | 60 | return $html; 61 | 62 | } 63 | 64 | public function save_extra_profile_fields() { 65 | return array( 'extra_field1' ); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /custom-multisite-signups.php: -------------------------------------------------------------------------------- 1 | ', "\n"; 62 | $selectors = '.mu_register #first_name, .mu_register #last_name'; 63 | if ( has_filter( 'cms_extra_fields_css_selectors' ) ) { 64 | echo apply_filters( 'cms_extra_fields_css_selectors', $selectors ); 65 | } else { 66 | echo $selectors; 67 | } 68 | 69 | echo ' { '; 70 | $css = 'font-size: 24px; margin: 5px 0; width: 100%;'; 71 | if ( has_filter( 'cms_extra_fields_css' ) ) { 72 | echo apply_filters( 'cms_extra_fields_css', $css ); 73 | } else { 74 | echo $css; 75 | } 76 | echo ' }', "\n", '', "\n"; 77 | 78 | $first_name = isset( $_REQUEST['first_name'] ) ? (string) $_REQUEST['first_name'] : ''; 79 | $html[] = ''; 80 | $html[] = ''; 81 | 82 | $last_name = isset( $_REQUEST['last_name'] ) ? (string) $_REQUEST['last_name'] : ''; 83 | $html[] = ''; 84 | $html[] = ''; 85 | $html[] = ''; 86 | $html = implode( "\n", $html ); 87 | 88 | if ( has_filter( 'cms_add_extra_signup_fields' ) ) { 89 | echo apply_filters( 'cms_add_extra_signup_fields', $html ); 90 | } else { 91 | echo $html; 92 | } 93 | 94 | } 95 | 96 | /** 97 | * Validate user signup data 98 | * 99 | * @param array 100 | * @return array 101 | * @filter hook returns array of errors 102 | */ 103 | public function _wpmu_validate_user_signup( $result ) { 104 | 105 | if ( empty( $_POST['first_name'] ) ) { 106 | $result['errors']->add( 'first_name', __( 'You must include a first name.' ) ); 107 | echo '

', $result['errors']->get_error_message('first_name'), '

'; 108 | } 109 | 110 | if ( empty( $_POST['last_name'] ) ) { 111 | $result['errors']->add( 'last_name', __( 'You must include a last name.' ) ); 112 | echo '

', $result['errors']->get_error_message('last_name'), '

'; 113 | } 114 | 115 | if ( preg_match( '/[^-a-zA-Z]/', $_POST['first_name'] ) or preg_match( '/[^-a-zA-Z]/', $_POST['last_name'] ) ) { 116 | $result['errors']->add( 'name', __( 'Names may only contain letters or hyphens.' ) ); 117 | echo '

', $result['errors']->get_error_message('name'), '

'; 118 | } 119 | 120 | if ( has_filter( 'cms_wpmu_validate_user_signup' ) ) { 121 | return apply_filters( 'cms_wpmu_validate_user_signup', $result ); 122 | } else { 123 | return $result; 124 | } 125 | } 126 | 127 | /** 128 | * Add values to wp_signups table 129 | * 130 | * @param array 131 | * @return array 132 | * @filter hook returns array of key, value pairs 133 | */ 134 | public function _add_signup_meta( $meta = array() ) { 135 | 136 | $fname = sanitize_text_field( $this->ucname( $_POST['first_name'] ) ); 137 | $lname = sanitize_text_field( $this->ucname( $_POST['last_name'] ) ); 138 | 139 | // create an array of custom meta fields 140 | $meta['custom_usermeta'] = array( 141 | 'first_name' => $fname, 142 | 'last_name' => $lname, 143 | 'display_name' => $fname . ' ' . $lname, 144 | ); 145 | if ( has_filter( 'cms_extra_signup_meta') ) 146 | $meta = $meta['custom_usermeta'] + apply_filters( 'cms_extra_signup_meta', $meta ); 147 | 148 | return $meta; 149 | 150 | } 151 | 152 | /** 153 | * Upon activation loops through wp_signups data to add to user_meta 154 | * 155 | * @param ($meta) array of key,value pairs. 156 | */ 157 | public function insert_meta_on_activation( $user_id, $email, $meta ) { 158 | 159 | // loop through array of custom meta fields 160 | foreach ( $meta as $key => $value ) { 161 | // and set each one as a meta field 162 | update_user_meta( $user_id, $key, $value ); 163 | } 164 | wp_update_user( array( 165 | 'ID' => $user_id, 166 | 'display_name' => $meta['display_name'], 167 | ) 168 | ); 169 | 170 | } 171 | 172 | /** 173 | * Add additional custom field to profile page 174 | * 175 | * @param - $user 176 | * @filter hook returns html 177 | */ 178 | public function show_extra_profile_fields ( $user ) { 179 | 180 | echo "\n", '

', _e( 'Extra Profile Info'), '

', "\n", '', "\n"; 181 | 182 | echo apply_filters( 'cms_show_extra_profile_fields', $user ); 183 | 184 | echo '', "\n", '', '
', "\n"; 185 | 186 | } 187 | 188 | /** 189 | * Update extra profile fields in admin page. 190 | * 191 | * @param - $user_id 192 | * @filter hook returns array of field names. 193 | */ 194 | public function save_extra_profile_fields( $user_id ) { 195 | 196 | //if ( ! current_user_can( 'add_users' ) ) { return false; } 197 | $extra_fields = array(); 198 | $extra_fields = apply_filters( 'cms_save_extra_profile_fields', $extra_fields ); 199 | foreach ( $extra_fields as $extra_field ) { 200 | update_user_meta( $user_id, $extra_field, $_POST[$extra_field] ); 201 | } 202 | 203 | } 204 | 205 | 206 | } //end class 207 | --------------------------------------------------------------------------------