Search code examples
phpwordpressprofile

WordPress Public User Profiles


I need to implement public user profiles for authors who use WordPress as CMS. Each profile should have an option to rate the author (5 stars) and put a comment on the author. T

I'm stuck at the following - it is clear how to do this with posts, but I just don't get what is user profile in WordPress is (or does it even exist). E.g. for sure I know that posts and pages exist as separate entities, but user profile - does it exist? Or should I just create a custom post type - user profile and that's it?


Solution

  • Like that question. As you can see in the WordPress database description users are not treated like posts/pages/custom post types in WordPress.

    https://codex.wordpress.org/Database_Description

    Posts are saved in the wp_posts table which has its own wp_postmeta table for saving additional information. Users are saved in the wp_users table which also has its own wp_usermeta table for saving additional information.

    If your authors are actually users which can log into WordPress in order to create and or publish posts you should not use a custom post type but create real authors as users.

    Implementing the author rating should be very simple - save the ratings for each user into the wp_usermeta table with the following function:

    update_user_meta( $user_id, $meta_key, $meta_value, $prev_value );
    

    With the comments it is a little bit more tricky - as you can see comments are saved having a post ID as well as an author ID. Probably its not a good idea to save comments connected to an author only in the wp_comments table as WordPress is not built for that. However you MAY find a solution using this approach.

    What I would recommend is the following: Save the comments belonging to an author in the wp_usermeta table, too. You could make em editable on the users profile page by adding some logic.

    As long as you do not expect thousands of comments per author its not a problem if you save a serialized array with comment date, comment title, comment author email, comment content, etc. into a single user meta field named "author_comments".

    Third possibility: Build a custom table for comments on authors with foreign key = user ID. Use the $wpdb class for populating it with the comment data (https://codex.wordpress.org/Class_Reference/wpdb).