Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question
add comment

1 Answer 1

up vote 10 down vote accepted

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.

share|improve this answer
    
So, what you are suggesting is create a custom post type and implement the loop to the author page. And then put up the comment form so that other users can post comment to that blank post? But the same comments will be shown to every users profile page. It can be avoided by creating function which will auto create custom post with the title of the username with wp_insert_post() when a user is registered. The author of the post could be the user. So, that when we pull the loop of post we make sure the post with the author=user shows. Am I in the right direction? Am I making it complicated? –  Towfiq Dec 31 '10 at 10:34
    
@Towfiq: Indeed, per user you create one post of your author custom post type. Then on the taxonomy-author.php template file, you also query for all posts of the current author, and thus "fake" your author page. You could also do it the other way around (use the author template and include the custom post), but then you need to trick the commenting form to use the right post id. –  Jan Fabry Dec 31 '10 at 10:54
    
Thank for your reply Fabry. I don't have very little knowledge about taxonomy. I tried to get something out of this page:codex.wordpress.org/Template_Hierarchy#Custom Taxonomies display But failed. can you tell me what taxonomy-author.php is and what will it do? and if I try the other way around, do you have any idea about tricking the post id?? -thanks –  Towfiq Dec 31 '10 at 11:25
    
@Towfiq: My mistake, it should not be taxonomy-author.php, but single-author.php. You don't create a custom taxonomy, but a custom post type. The single-author.php is the template file that is used to display a single post of the author custom post type. I suggest you go this way, it's the easiest (you can also use paging (/page/2/) if you have many posts from that author). –  Jan Fabry Jan 1 '11 at 10:53
1  
@Towfiq? How did I miss that when I reviewed my code? Hmm, sorry. Anyway, it should how helpful it is when you tell me where the error occurred. :) I'm also surprised it worked on my end without error. BTW, I was using a different hook before so that was remnant code. Here's what should fix it: if ($person->post_type=='towfiq-person') {. Let me know... –  MikeSchinkel Jan 3 '11 at 21:19
show 24 more comments

protected by Community Mar 27 '12 at 21:01

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.