Commenting in user profile page?

I am trying to build custom user profile with the guidance of this tutorial: How to make a WordPress profile page

I have successfully implemented it to my theme, everything is working well.
Now what I want to achieve is to get the comment template in the user profile page, where other registered user can post comment on his profile page, kinda like facebook wall or last.fm shoutbox.

Read More

I am trying it like this:

In the author page I am added this line:

<?php comments_template(); ?>

But it does not show up.
Then I tried this way: Get WordPress comments outside of WordPress

It adds the comment template alright but, does not work. When you click on on the submit button it redirects to a blank page.

I think the goal is not achievable easily, it requires custom database creation for each user to store the comments, since the comment system only stores comments of certain page or post, not for any other page like archive or author page.

If anyone can show me the right direction, I will be forever grateful.

Thanks
Towfiq I.

Related posts

Leave a Reply

2 comments

  1. Hi @Towfiq:

    Comments are related in the database to Posts. You’ll have to do a lot of work to get Comments to relate to Users.

    Have you considered creating a Custom Post Type for Users and then use either a user_meta field to store the post_id, or a postmeta field to store the user_id, or both? If you did that then you would get the comments with no effort at all.

    UPDATE

    What follows is code developed after our discussion in the comments.

    I’ve been meaning to write something like this for a long time but your question finding got me to make it a priority. I’ve created a 'towfiq-person' custom post type for you and I’ve set it up to automatically add a Person post whenever a User is added and it uses the email address as the associating key in a post custom field called '_email'.

    It also associates a User with an appropriate email address to the Person post if a User is added or updated with the same email as an existing Person (this may or may not be a good idea.) And it cross-references User with Person and Person with User using postmeta and usermeta fields '_user_id' and '_person_id', respectively.

    These are of course business rules I chose to implement but they may turn out not to be appropriate for your use-case in which case you may need to modify them. You also may find ways that WordPress allows these two to get out of sync but it’s hard to know that without exhaustive testing. If you find issues you can always look to update the logic to resolve them.

    You can copy the following code to your theme’s functions.php file:

    class Towfiq_Person {
      static function on_load() {
        add_action('init',array(__CLASS__,'init'));
        add_action('wp_insert_post',array(__CLASS__,'wp_insert_post'),10,2);
        add_action('profile_update',array(__CLASS__,'profile_update'),10,2);
        add_action('user_register',array(__CLASS__,'profile_update'));
        add_filter('author_link',array(__CLASS__,'author_link'),10,2);
        add_filter('get_the_author_url',array(__CLASS__,'author_link'),10,2);
      }
      static function init() {
        register_post_type('towfiq-person',
          array(
            'labels'          => array('name'=>'People','singular_name'=>'Person'),
            'public'          => true,
            'show_ui'         => true,
            'rewrite'         => array('slug' => 'people'),
            'hierarchical'    => false,
            //'supports'        => array('title','editor','custom-fields'),
          )
        );
      }
      static function get_email_key() {
        return apply_filters( 'person_email_key', '_email' );
      }
      static function profile_update($user_id,$old_user_data=false) {
        global $wpdb;
        $is_new_person = false;
        $user = get_userdata($user_id);
        $user_email = ($old_user_data ? $old_user_data->user_email : $user->user_email);
        $email_key = self::get_email_key();
        $person_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='%s' AND meta_value='%s'",$email_key,$user_email));
        if (!is_numeric($person_id)) {
          $person_id = $is_new_person = wp_insert_post(array(
            'post_type' => 'towfiq-person',
            'post_status' => 'publish',   // Maybe this should be pending or draft?
            'post_title' => $user->display_name,
          ));
        }
        update_user_meta($user_id,'_person_id',$person_id);
        update_post_meta($person_id,'_user_id',$user_id);
        if ($is_new_person || ($old_user_data && $user->user_email!=$old_user_data->user_email)) {
          update_post_meta($person_id,$email_key,$user->user_email);
        }
      }
      static function wp_insert_post($person_id,$person) {
        if ($person->post_type=='towfiq-person') {
          $email = get_post_meta($person_id,self::get_email_key(),true);
          if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $user = get_user_by('email',$email);
            if ($user) { // Associate the user IF there is an user with the same email address
              update_user_meta($user->ID,'_person_id',$person_id);
              update_post_meta($person_id,'_user_id',$user->ID);
            } else {
              delete_post_meta($person_id,'_user_id');
            }
          }
        }
      }
      static function get_user_id($person_id) {
        return get_user_meta($user_id,'_user_id',true);
      }
      static function get_user($person_id) {
        $user_id = self::get_user_id($person_id);
        return get_userdata($user_id);
      }
      static function get_person_id($user_id) {
        return get_user_meta($user_id,'_person_id',true);
      }
      static function get_person($user_id) {
        $person_id = self::get_person_id($user_id);
        return get_post($person_id);
      }
      static function author_link($permalink, $user_id) {
        $author_id = get_user_meta($user_id,'_person_id',true);
        if ($author_id) // If an associate is found, use it
          $permalink = get_post_permalink($author_id);
        return $permalink;
      }
    }
    Towfiq_Person::on_load();
    

    If you need any clarifications to what I did and why, just ask in the comments.

  2. Just add a custom post type loop inside author.php and use that custom post’s comment form. I’ve done it many times and it works great.

    <?php /* Display the author's comments from the custom post type (AAA) */ ?>
    <?php
    $authorid = get_the_author_meta( ID, $userID );
    $args=array('author' => $authorid,'post_type' => 'AAA', 'numberposts' => -1);
    $cquery=new WP_Query($args);
    if($cquery->have_posts()):
    while($cquery->have_posts()):
    $cquery->the_post();
    ?>          
    <div class="comments-area">
        <?php comments_template(); ?>
    </div>
    <?
        endwhile;
            wp_reset_postdata();
        endif;
    ?>
    

    https://github.com/pjeaje/code-snippets/blob/gh-pages/GP%20author.php%20with%20multiple%20loops