Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress - Allowing comments on author pages

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

like image 624
Zach Nicodemous Avatar asked Dec 09 '15 11:12

Zach Nicodemous


1 Answers

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();
like image 58
Anand thakkar Avatar answered Sep 29 '22 00:09

Anand thakkar