6
votes

I need to be able to allow users to leave comments on author profile pages.

I came across another answer here that has given me a basic outline of what needs to be done to allow it: https://wordpress.stackexchange.com/questions/8996/author-page-comments-and-ratings

That said, I am unsure how to proceed with getting it implemented. I have created a custom post type to hold the comments, but I do not know how to make it so that each time a user/author signs up to our site (we are building a site with open registration), a post gets created in the custom post type to hold comments, and then automatically associated to their user profile.

Would much appreciate a more detailed answer than what was provided on the linked question, so that I am able to understand exactly how to get this up and running.

Many Thanks

3

3 Answers

3
votes

Actually you need to simply hook the Stuff with the user_register Action like

function my_user_register($user_id){

    $user = get_user_by( 'id', $user_id );

    /**
     * if required you can limit this profile creation action to some limited 
     * roles only
     */

    /**
     * Created new Page under "user_profile_page" every time a new User 
     * is being created
     */

    $profile_page = wp_insert_post(array(
        'post_title'        => " $user->display_name Profile ", // Text only to Map those page @ admin
        'post_type'         => "user_profile_page", // Custom Post type which you have created 
        'post_status'       => 'publish',
        'post_author'       => $user_id,
    ));

    /**
     * Save the Profile Page id into the user meta
     */
    if(!is_wp_error($profile_page))
        add_user_meta($user_id,'user_profile_page',$profile_page,TRUE);
}

/**
 * Action which is being trigger Every time when a new User is being created
 */
add_action('user_register','my_user_register');

Above code is something which you are actually missing from the original post https://wordpress.stackexchange.com/questions/8996/author-page-comments-and-ratings So after adding the above code you simple need to follow that over the author.php same like that code

$profile_page = get_the_author_meta('user_profile_page');

global $post;
$post = get_post($profile_page);

setup_postdata( $post ); 

//fool wordpress to think we are on a single post page
$wp_query->is_single = true;
//get comments
comments_template();
//reset wordpress to ture post
$wp_query->is_single = false;

wp_reset_query();
0
votes

You can hook a new filter to wp_insert_user function (if you're using it to insert user on registration step) using insert_user_meta hook, so, after creating new user, this filter will be called on meta array before saving it. Here is the example code that might be helpful:

<?php
add_filter('insert_user_meta', 'nl_associate_cp_to_user', 10, 3);
function nl_associate_cp_to_user($meta, $user, $update ){
    $meta['cpid_for_comments'] = nl_get_new_cpid_for_comments();
    return $meta;
}

function nl_get_new_cpid_for_comments(){
    $data = array(
        'post_type'    => 'CCPT',
        'post_title'    => 'Stub Post',
        'post_content'  => '',
        'post_status'   => 'publish',
        'post_author'   => 1,
    );

    $post_id = wp_insert_post( $data );
    if(!is_wp_error($post_id)) return $post_id; 
}
?>
0
votes

You Can update your user-author.php file with the below code.

<?php
           //save blog Id
            $blog_id = $post->ID;
           // now map your post with author post
            $author_post_id = get_user_meta($user_id, author_post_id,blog);
            query_posts("p=$author_post_id");
            the_post();
           //Your are in a single post page
           $wp_query->is_single = blog;
          //get comments
          comments_template();
          //reset wordpress to particular post
          $wp_query->is_single = false;
          query_posts("p=$blog_id");
         the_post();
    ?>

You need to understand here we assume with blog id for the particular blog. please let me know if there are any other issue you are facing.