The user profile of WordPress can be fairly easily adapted to add your own values. So you can add the necessary fields according to your requirements. Here is how you do it, we add a field for the address and the content will be stored in the database. Various hooks in WordPress make sure that you only have to worry about the fields.
The function to store the entries is fb_save_custom_user_profile_fields()
and here it is important to check the rights of each active user, only if the rights are available, in this case for editing users (edit_user
), then data may be stored.
The actual saving of data takes place via update_usermeta()
.
It can pass all kinds of fields of course, the input field is just an example. It is advisable to outsource the functions in a Plugin, an alternative is also the functions.php
of the Theme, but is certainly not the best way.
Each function of the small sample was referred to two hooks. Because WordPress differentiate between editing of user profiles and update personal data. This is done using the constant IS_PROFILE_PAGE
. On default this constant is on TRUE and so would the hooks show_user_profile
and personal_options_update
be enough to bring in new fields and save them. But this can vary depending on the installation and this way the admin cannot maintain these new fields. In particular, two more hooks are needed. Otherwise there might be many cases, where the admin has to maintain data which the user doesn't even see in his profile.
function fb_add_custom_user_profile_fields( $user ) { ?> <h3><?php _e('Extra Profile Information', 'your_textdomain'); ?></h3> <table class="form-table"> <tr> <th> <label for="address"><?php _e('Address', 'your_textdomain'); ?> </label></th> <td> <input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e('Please enter your address.', 'your_textdomain'); ?></span> </td> </tr> </table> <?php } function fb_save_custom_user_profile_fields( $user_id ) { if ( !current_user_can( 'edit_user', $user_id ) ) return FALSE; update_usermeta( $user_id, 'address', $_POST['address'] ); } add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' ); add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' ); add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' ); add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );
© WP Engineer Team, All rights reserved (Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)
Comment Length Limiter
If you have used Twitter, then you know that you are only allowed to type 140 characters in a single Tweet. There is a nice little number below the text field indicating how much is left to write.
It would be nice to have this feature for WordPress comments too. Or any text field, for that matter. It can be done with the following piece of JavaScript:
https://gist.github.com/1422754
The first three
var
s below the// configure
comment can be edited.comment_input
is the DOM element of the text field.submit_button
is the DOM element for the button to submit the form.Finally,
comment_limit_chars
is the amount of characters allowed.This snippet automatically inserts a
div
tag below the text field and updates the character count when the user types. Thesubmit_button
is optional. Set thevar
tonull
if you don't want it to be grayed out.Please keep in mind that this only validates the input on the client side. If you have to rely on the maximum text length, like Twitter does, you need to check the length on the back end side with PHP too.
Guest Post
This post is written by Eric Teubert - www.satoripress.com and is a post in our Advent Calendar on WP Engineer about WordPress.
Thank you very much from my part to Eric.
If you also like to have your interesting post published on our website, please let us know on our contact page. Of course we will appreciate your contribution!
© WP Engineer Team, All rights reserved (Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)