Skip Navigation

[Resolved] Single author template / Profile page

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to automatically create a profile page for each new User who signs up with CRED.

Solution: You can use the cred_save_data API to automatically create a post after registration. Create a "Profile" post type, and add the following custom code to functions.php:

add_action('cred_save_data', 'ts_new_user_profile_action',10,2);
function ts_new_user_profile_action($user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==1234)
    {
      // Create post object
      $my_post = array(
        'post_title'    => $_POST['first_name'] . ' ' . $_POST['last_name'],
        'post_content'  => '',
        'post_status'   => 'publish',
        'post_author'   => $user_id,
        'post_type' => 'profile'
      );
 
      // Insert the post into the database
      wp_insert_post( $my_post );
    }
}

Replace 1234 with the numeric ID of the CRED registration form, replace "profile" with the slug of the post type you want to create, and modify the post_title if you want the title of the post to be anything other than the new User's name.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/wp_insert_post/

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#653321

Hi,
I've created a membership site with 2 custom post types. 1 for creating a job and 1 for sending quotes. This works all fine. I then created a WordPress archive for authors. On this archive you need to be able to click on an author in the loop and go to his individual author template / Profile page.

Here comes in problem 1: I've had to create a view instead of the standard WordPress archive because I want only 1 user role to be seen. Whenever I click on the link to go to the individual authors page I end up on the current logged in users author page.

Problem 2:
I've created an author.php template within my childtheme to serve this. This isn't a problem but I cannot seem to show the user fields from the author within the loop. So I click on an author --> info that should be seen is that authors info.

I've created several custom user fields such as "beroep" (profession in english) and when I use the following to show this:

<?php echo types_render_usermeta("beroep"); ?>

I've also tried to wrap it in a shortcode:

<?php echo do_shortcode("[wpv-user field='first_name']"); ?>

either nothing shows up or it shows the info from the current logged in user. Am I approaching this the wrong way? Or should I just create a standard WordPress archive for authors instead of a view?

Link to a page where the issue can be seen: hidden link (problem 1)

#653869

Whenever I click on the link to go to the individual authors page I end up on the current logged in users author page.
Can you share the code you have placed in this View's Loop Output, as well as any Content Templates used in that Loop? I would like to see how you have configured this link.

either nothing shows up or it shows the info from the current logged in user
When you want to display usermeta information from a specific User, you must use the 2nd attribute of the function call to pass in an array of arguments. One of those arguments, user_is_author, can be used in the context of an author archive to indicate you want the meta field of the archive author and not the current logged-in User:

<?php echo types_render_usermeta("beroep", array( "user_is_author" => true ) ); ?>

Click the orange "+More" button here to reveal some additional information about this API:
https://toolset.com/documentation/customizing-sites-using-php/functions/#textfield

#696466

Hi,

I've tried the code you showed me but nothing still shows up in the author.php template when used. I've tried several methods and slugs. Is there a different way to create user profiles? Or a more effective way?

The code used for the author link (to the individual author page). This should link to the individual author clicked within the loop.

<a href="[wpv-post-author format='url'] "> [wpv-post-author meta="user_firstname" format="meta"] [wpv-post-author meta="user_lastname" format="meta"] </a>
#710503

If you revert to the default author.php archive template file, then you can go to Toolset > WordPress Archives, and create a WordPress Archive for the Author archive. In the Loop output editor of a WordPress Archive, you can add any information you want just above the wpv-loop tags. This can serve as the Author profile page. However, it should be noted that at least one post must be published by the User for this archive to be active.

#714508

I see. But I want to create a user profile. Users don't create posts. It's a job board type of site. So the user's information should be shown on the single author page. How can this be realised when the users doesn't have a post then?

#718431

WordPress does not show an author archive for Users who have no published posts. This isn't something controlled by Toolset it's just how WordPress works. One workaround is that when a User registers, you can automatically publish a post in some post type that isn't shown anywhere on the site, and set the new User as the author. You can use the WordPress API wp_insert_post to create a post, and if your Users register with CRED you can use the cred_save_data hook.
https://developer.wordpress.org/reference/functions/wp_insert_post/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
I can provide some assistance with this CRED hook if you'd like.

Another workaround is that you can create a custom Page with a View of Users, filtered by username, set by a URL parameter. It's not a pretty URL, but something like http://yoursite.com/user-profile/?username=someusername would show the profile of the User with the login "someusername".

Another option is that you do not use WordPress Archives at all, instead you create some custom post type for Profiles. When a User registers, you create a new Profile post, and link it to the User somehow (a custom field holding the User ID, for example). The URL to a profile would be something like http://yoursite.com/profile/some-unique-profile-slug.

#718627

Wouldn't option 1 create unnecessary content on the site? Sure I could no index the posts but then again wouldn't that just create unwanted pages? And the bigger the site gets the more clutter it creates? I'm saying this because I've had experience with creating sites with a big user base. I expect this site to also have quite a big user base when done.

Option 2 seems reasonable but isn't that almost the same as what I'm doing now? Currently I've created a view of users of a certain user role. However it's not possible to visit their single author page nor does it show their (custom) user information.

Option 3 seems like the better solution since I could then just turn off author archive pages. However this option would require a few things:

- Upon registering a user from a certain user role the custom post should be made automatically. If I create a custom post type called "Profiles" how could I connect this to user registrations? Especially user registrations for a certain user role(s)?

- Creating the page itself wouldn't be a problem and displaying the fields neither since I could just use the regular fields and views I'm currently using right?

- If a user would be in his "my account" area and he would want to view his profile is there a possibility to show his linked profile from within here?

#720067

Wouldn't option 1 create unnecessary content on the site?
Some published content is necessary if you want to have anything show up at the User's author archive URL. If that published content makes your site's content size unmanageable, then an author archive is probably not the best solution to your problem.

Option 2 seems reasonable but isn't that almost the same as what I'm doing now? Currently I've created a view of users of a certain user role. However it's not possible to visit their single author page nor does it show their (custom) user information.
Again, if a User is not the author of any published content then there is no Author archive page. Toolset can't help change that behavior. If you have stored custom fields on the User profile, then a View that includes that User in the results should be able to show any of those custom fields. So I'm not sure why this would not be working, I may need to take a closer look.

- Upon registering a user from a certain user role the custom post should be made automatically. If I create a custom post type called "Profiles" how could I connect this to user registrations? Especially user registrations for a certain user role(s)?
Either you can set the User to be the Author of the Profile post (which ironically eliminates the need for a Profile post, because then the User is an Author and an Author archive URL will be created), or you can use a custom field that stores an ID reference. For example, on the User you could store the related Profile post's ID in a custom field, or in the Profile post you could store the related User's ID in a custom field.

- Creating the page itself wouldn't be a problem and displaying the fields neither since I could just use the regular fields and views I'm currently using right?
You can use regular fields and Views to display information about Profile posts, but I'm not sure whether or not you will need to create new ones...I'm not 100% clear on everything you need to accomplish.

- If a user would be in his "my account" area and he would want to view his profile is there a possibility to show his linked profile from within here?
I'm not sure what the "my account" area is, but if you have the ability to insert content like a View, then you can create a View that shows any information from the linked Profile. That View can be filtered by Profile ID, and you can pass the profile reference value into the View filter using a shortcode attribute, something like this:

[wpv-view name="your-profile-view-slug" ids="[wpv-user field='wpcf-profile-id']"]

This technique is called passing arguments into Views and is described in detail here:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

#777143

Oops, I thought I posted my reply.

I'd like to create a custom post type called "Profiles" with a standard custom template that's used for it. But how would I make it so that newly registered users (with a certain user role) auto- create a profile (post) within this new custom post type?

Is it even possible to do this upon registration?

#779633

Yes, this is possible with the CRED API. Here is some sample code you can add to your child theme's functions.php file:

add_action('cred_save_data', 'ts_new_user_profile_action',10,2);
function ts_new_user_profile_action($user_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==1234)
    {
      // Create post object
      $my_post = array(
        'post_title'    => $_POST['first_name'] . ' ' . $_POST['last_name'],
        'post_content'  => '',
        'post_status'   => 'publish',
        'post_author'   => $user_id,
        'post_type' => 'profile'
      );

      // Insert the post into the database
      wp_insert_post( $my_post );
    }
}

Change 1234 to match the numeric ID of the new User CRED form, change 'profile' to match the slug of your profile post type (if it's not profile), and you can modify the post_title attribute if you want the Profile post title to be anything other than the new User's name.

#821527

Thanks. That did the trick. When the user creates an account an automatic post is made within the new custom post type "Profiles". I then created a content template for the single page which then shows the profile of the user.