WordPress is a great tool to use for managing content. It’s really simple for developers, for managers and for users to use, especially when using a great theme. But if there is one thing that I think that needs to improve yet is its user management settings. I mean, even basic functions need to be coded, like editing a user’s profile fields, add roles, edit capabilities, export user data. All of those need plugins or a little coding to get working.
Here we’ll see 5 amazing and simple tips that you can use right now and greatly improve your user management and you can use all this additional data to serve your loyal readers better and provide more focused content.
We’ll cover custom user fields to history functionality, and while we’re at it you’ll learn a lot about WordPress user management functions.
So, let’s rock!
What will we see?
We’ll focus on user specific data, so what we’ll see is:
- How to add extra registration fields
- How to add and remove profile fields
- Export the user data
- Bulk export users data
- Create a web history of the user
1. How to add extra registration fields
Sometimes we want to know our users better. Where they live, if they want to receive our newsletter, or just store their IP. Well, to get this working the easiest way is to use one of these:
PieRegister
It allows you to customize the user registration screen. Pretty useful and easy to change.
What I like most is its simplicity. If you want to just get started, this one is your best choice.
s2Member
On the other side, you may use a plugin like s2member which gives you full control over user’s capabilities and data. But it requires a little more setup and will change a lot of things in your wp-admin. So you can edit each posts visibility for free or premium users.
Maybe it’s too much if you just want to add more user registration fields, but it gives you much better control over user data and content.
2. How to add and remove profile fields
Truth be told, the default user profile fields are more markup than anything useful. But WordPress has an easy way to edit them.
Let’s say instead of Yahoo IM, you want to add a Twitter field. Or just remove it. Well, this can be done with the filter ‘user_contactmethods’, which can be added as plugin or in your theme’s functions.php file. Do it this way:
<!--?php--><?php function wd_edit_fields( $contactmethods ) { //new fields $contactmethods['twitter'] = 'Twitter'; $contactmethods['tumblr'] = 'Tumblr'; $contactmethods['skype'] = 'Skype'; unset($contactmethods['aim']); unset($contactmethods['jabber']); unset($contactmethods['yim']); return $contactmethods; } add_filter('user_contactmethods','wd_edit_fields',10,1); ?>
3. Export the user data
Now we have saved user data, but it can’t be seen. What we’ll need is a function to export this when we want (in a custom page, for instance).
Our function will get user data directly from the wp_usermeta table, and remove unnecessary data (used just by WordPress, but keep them if you want to export all user data).
This function will do the trick:
<!--?php--><?php function get_all_user_meta( $id ) { $to_remove = array("first_name", "last_name", "nickname", "rich_editing", "comment_shortcuts", "admin_color", "show_admin_bar_front", "wp_capabilities", "wp_user_level", "wp_dashboard_quick_press_last_post_id", "wp_user-settings", "wp_user-settings-time", "closedpostboxes_post", "metaboxhidden_post"); //all default and undesired fields $remove = ""; if (! empty($to_remove) && is_array($to_remove) ) { foreach ($to_remove as $item) { $remove .= "AND meta_key != '$item' "; } } global $wpdb; $qry = "SELECT meta_key, meta_value FROM wp_usermeta WHERE user_id = $id $remove"; $myrows = $wpdb->get_results( $qry ); return $myrows; } $data = get_all_user_meta( 1 ); foreach( $data as $field ) { if( ! empty ($field->meta_value) ) echo "<strong>".$field->meta_key."</strong>: ".$field->meta_value."<br />"; } ?>
4. Bulk export user data
Now if you want to move from WordPress to another CMS you may need to hire a programmer just to figure out how to export all user data. Ok, we’ll save you from this crazy work and give you a function to see all users with all their custom data!
What we have to do is get the previous function and run it for every user_id inside the WordPress wp_users table. Then you can import this content or format it according to your new CMS standards.
<!--?php--><?php function get_all_user_meta( $id ) { $to_remove = array("first_name", "last_name", "nickname", "rich_editing", "comment_shortcuts", "admin_color", "show_admin_bar_front", "wp_capabilities", "wp_user_level", "wp_dashboard_quick_press_last_post_id", "wp_user-settings", "wp_user-settings-time", "closedpostboxes_post", "metaboxhidden_post"); //all default and undesired fields $remove = ""; if (! empty($to_remove) && is_array($to_remove) ) { foreach ($to_remove as $item) { $remove .= "AND meta_key != '$item' "; } } global $wpdb; $qry = "SELECT meta_key, meta_value FROM wp_usermeta WHERE user_id = $id $remove"; $myrows = $wpdb->get_results( $qry ); return $myrows; } function get_all_user_ids() { global $wpdb; $qry = "SELECT ID FROM wp_users"; $myrows = $wpdb->get_results( $qry ); return $myrows; } $users = get_all_user_ids(); foreach( $users as $user ) { $data = get_all_user_meta( $user->ID ); echo "<h2>User #".$user->ID." data</h2>"; foreach( $data as $field ) { if( ! empty ($field->meta_value) ) echo "<strong>".$field->meta_key."</strong>: ".$field->meta_value."<br />"; } } ?>
5. Create a web history of the user
Sometimes user behavior is completely nonlinear. I know, you publish your posts with a carefully thought out order, but people just click everywhere and sometimes they stop thinking “which one was the cool post I was reading 5 minutes ago?”.
Same thing may happen if you run an e-commerce store over WordPress. Users navigate through products and sometimes the very first one was the best for them.
So for these times, I would recommend you build a user history.
First you should define how many items you want to store, and this will depend on your site’s size. We’ll do this with the last 30 visited pages (easily changed).
Then, you’ll need to add this snippet to your functions.php or plugin file, then you just call show_history and you’ll be fine :)
<?php add_filter('wp_head', 'wd_history'); function wd_history() { $current_user = wp_get_current_user(); // logged in? if ( ! empty ($current_user->ID) ) { $i = get_user_meta($current_user->ID, "wd_i", true); $i++; //update fields update_user_meta($current_user->ID, "wd_i", $i); update_user_meta($current_user->ID, "wd_page_".$i, "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"); //delete older delete_user_meta( $current_user->ID, "wd_page_".($i-30) ); } } function show_history() { $current_user = wp_get_current_user(); // logged in? if ( ! empty ($current_user->ID) ) { $i = get_user_meta($current_user->ID, "wd_i", true); echo "<ul>"; $end = $i - 30; while( $i > $end ) { $url = get_user_meta($current_user->ID, "wd_page_".$i, true); $pid = url_to_postid( $url ); if ( ! empty ($pid) ) { $page = get_post($pid); $title = $page->post_title; } else { $title = "Home"; } echo "<li><a href='$url'>$title</a></li>"; $i--; } echo "</ul>"; } } show_history(); ?>
Other experiments
Did you know that it’s possible to create shopping cart functionality with user meta data? Well, we haven’t covered this because the article would get too big, but we will cover this soon :)
Now it’s your turn!
What do you think? Is there another user management function that you want to share with us? Just comment! :)
No hay comentarios:
Publicar un comentario