18May/101
Adding Meta Fields to WordPress Registration
I did a lot of digging this week trying to figure out how to add additional fields to the user registration/edit forms on WordPress 4.9 and I wanted to do this without hacking the core. While there is a lot of information out there. no one really goes into the details on adding the fields.
To do this, was actually quite simple, open your theme's function.php file and add the following add_action hook's.
1 2 3 4 | add_action( 'show_user_profile', 'show_extra_profile_fields' ); add_action( 'edit_user_profile', 'show_extra_profile_fields' ); add_action( 'personal_options_update', 'update_extra_fields' ); add_action( 'edit_user_profile_update', 'update_extra_fields' ); |
the add_action functions hooks your code to fire when the events for showing/updating the user data is fired in the core. for more info on the add_action function call see the wordPress Codex.
WordPress Codex: add_action
WordPress Codex: Plugin API/Action Refrence
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | function update_extra_fields( $user_id ) { if ( !current_user_can( 'edit_user', $user_id ) ) return false; update_usermeta( $user_id, 'city',$_POST['city'] ); update_usermeta( $user_id, 'state',$_POST['state'] ); update_usermeta( $user_id, 'zip',$_POST['zip'] ); } <?php function my_show_extra_profile_fields( $user ) { ?> <table class="form-table"> <tr> <th><label for="city">City</label></th> <td><input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ) ?>" class="regular-text" /><br /></td> </tr> <tr> <th><label for="state">State</label></th> <td><input type="text" name="state" id="state" value="<?php echo esc_attr( get_the_author_meta( 'state', $user->ID ) ) ?>" class="regular-text" /><br /></td> </tr> <tr> <th><label for="zip">Zip</label></th> <td><input type="text" name="zip" id="zip" value="<?php echo esc_attr( get_the_author_meta( 'zip', $user->ID ) ) ?>" class="regular-text" /><br /></td> </tr> </table> <?php } |
Tagged as: Meta Fields, PHP, WordPress
Leave a comment
Sponsors
Categories
- CakePHP (2)
- Classic ASP (VB Script) (14)
- CSS (2)
- HTML5 (1)
- Javascript Libraries/Frameworks (2)
- SmarterTrack (1)
- Software (3)
- T-SQL (4)
- VB.Net (1)
- WordPress (3)
Site Links
Blogroll
- Codango Web Development Reviews, Resources, and Inspiration.
- tuts+ From graphics to web development, audio to video and more, get the skills you want from our family of tutorial and resource sites.
- W3Avenue advice & resources for rapid web development
- WebAppers Hunting the Best Open Source Resources for Web Developers
June 11th, 2010 - 16:08
Nice post and this fill someone in on helped me alot in my college assignment. Thank you seeking your information.