Skip Navigation

[Resolved] See User Profile after Registering User with Cred

This support ticket is created 3 years, 10 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
#1893223

I want the user see their profile after registering. Just like in Linkedin. The profile should be available in the url: hidden link

But when I use the Post form to create a registration form, I do not see an option for 'display post'. This option is available for all custom post type. What am I missing here? Is there no way to create a custom user post type or display user profile with Toolset.

May be I need to create a custom post type named user-profile and insert it during the registration process. Just like here: https://toolset.com/forums/topic/have-a-band-admin-role-to-manage-cpt-band-listing-page/

But then my question is, who will be the author of this "inserted" post? Admin or the user who just registered.

I also want to create relationship between "user-profile" post type and the other CPTs that I have, so it is important the post under user-profile post type is for the newly registered user and not me.

Can you please help?

#1893259

It seems like the 'post_author' => $post_id serves the job of making the registered user as author. But how does $post_id store author info. It seems like it stores post ID as per https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1893399

So I did more digging on this and found that I could use 'user_register' hook to create a custom post for a Toolset generated post type. (reference: https://developer.wordpress.org/reference/hooks/user_register/)

The code:

add_action( 'user_register', 'create_portfolio_post', 10, 1 );

function create_portfolio_post( $user_id )
{
// Get user info
$user_info = array();
$user_info = get_userdata( $user_id );
// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_status' => 'publish',
'post_type' => 'portfolio' // <- toolset created CPT
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
//}
}

I have three questions:
(1) Is is okay to create posts in toolset created CPTs with user_register hook?
(2) Does the answer to (1) change whether I use a cred form for registration?
(3) If (2) is no, then what else could I be doing wrong to create a custom post on registration?

Thanks a ton!

#1893447

Hello, it sounds like you are creating a post type that will act as a proxy for Users. I have answers to your questions below, and we have some related information about this process in a document you might find helpful: https://toolset.com/documentation/legacy-features/views-plugin/how-to-create-custom-searches-and-relationships-for-users/

(1) Is is okay to create posts in toolset created CPTs with user_register hook?
Yes, it is okay in general. You should be aware that this will create a post automatically any time a User is registered, even if the User is not created by a Toolset Form. If you want to limit the post generation process to be triggered only by a Form, you could use the cred_save_data hook instead of the user-register hook. The cred_save_data hook can be easily configured to fire when submitting a specific Form, or submitting specific Forms. If you use the user_register hook, the automatic process could be triggered in any number of ways, like when a User is created directly in wp-admin, or when a User is created by some 3rd-party plugin like a contact form.

(2) Does the answer to (1) change whether I use a cred form for registration?
I'm not sure I fully understand the question, but see my previous answer. If you use a Form for registration, you can use the cred_save_data hook instead to limit this automatic post creation process to be triggered only by a specific User Form. If you want to trigger the automatic process regardless of how the User is created, the user_register hook is probably preferred.

(3) If (2) is no, then what else could I be doing wrong to create a custom post on registration?
I don't think you're doing anything inherently wrong by creating the custom post on registration. The process you described seems logical. If you want to redirect to the new custom post after registration with Forms, you'll need some custom code to modify the redirection URL because there is nothing built-in to User Forms that will allow you to redirect to a post that was created programmatically as you described. In the User Form settings, you must choose to redirect to some post or page after submission, otherwise the redirection API will not be triggered.

Basic API implementation starter script:

add_filter('cred_success_redirect', 'tssupp_redirect_to_generated_profile',10,3);
function tssupp_redirect_to_generated_profile($url, $post_id, $form_data)
{
    $forms = array( 123, 456 );
    if ( in_array( $form_data['id'], $forms ) ) {
       // add your custom code here
       // first, query profile posts by author id and get the results
       // get the permalink of the matching profile post
       // return the permalink of that profile post
    }
   // otherwise, return the default redirect url
   // do not edit or delete the following code
    return $url;
}

Replace 123, 456 with a comma-separated list of User Form IDs where you want to apply this customized redirection, then add your custom code to query profile posts by author id and return the permalink of the profile post.

Let me know if you have follow-up questions about this, if you need more details about creating the Profile post query, etc.

#1893465

Thanks a ton. I am working on the redirect one and will let you know if I need in any help. But before that, I am unable to create a post with e hook we just discussed. The post type name portfolio has been created and I am able to create a custom post from the admin page. But through the hook, it does not seem to work. I want to know if I am doing this wrong somewhere in the post type settings or in the code. As I am assigning the post

I have not added any custom taxonomy to the CPT either.

add_action( 'user_register', 'create_portfolio_post', 10, 1 );

function create_portfolio_post( $user_id )
{
// Get user info
$user_info = array();
$user_info = get_userdata( $user_id );
// $user_roles = $user_info->roles;

// New code added
// $this_user_role = implode(', ', $user_roles );
//if ($this_user_role == 'subscriber')
//{

// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_author' => $user_id,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'portfolio' // <- change to your cpt
);
// Insert the post into the database
wp_insert_post( $user_post );
//}
}

#1894335

Check the notes for wp_insert_post here: https://developer.wordpress.org/reference/functions/wp_insert_post/#more-information
post_title and post_content are required

That's the first thing I would check, since it seems your code does not include post_content. If you want to have empty post contents, pass in an empty string for the post_content argument:

$user_post = array(
'post_title' => $user_info->nickname,
'post_content' => '',
'post_author' => $user_id,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'portfolio' // <- change to your cpt
);

If that doesn't solve the problem, you may need to turn on error logging to determine the issue.
- Is the create_portfolio_post function called as expected? Add an error log statement inside the function and see if it is written out to the logs after registration.
- Is the $user_id variable set to the new User's ID as expected? Add an error log statement that includes the value of $user_id.
- Are there any syntax or other errors thrown during the post insertion process? Check the logs for more information.

#1894379

Thanks for that tip. I did that it still did not work. Now I am following your remaining steps.

add_action( 'register_new_user', 'create_portfolio_post', 10, 1 );

function create_portfolio_post( $user_id )
{
// Get user info
$user_info = array();
$user_info = get_userdata( $user_id );
// $user_roles = $user_info->roles;

// New code added
// $this_user_role = implode(', ', $user_roles );
//if ($this_user_role == 'subscriber')
//{

// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_author' => $user_id,
'post_content' => $user_info->nickname,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'portfolio' // <- change to your cpt
);
// Insert the post into the database
wp_insert_post( $user_post );
//}

#1894415

Chris,

I ran the error log as you suggested and could not find any error. I can upload it here but want share on a private message.

This trigger to create a custom post on registration is fundamental to the functioning of my product as wordpress does not let user the user. Can you please look into this and help me figure this out?

Regards,
Himanshu

#1895309

I ran the error log as you suggested and could not find any error. I can upload it here but want share on a private message.
Sure, I can activate private reply fields here. Add any placeholder information required to submit the reply.

For your assistance, I have added some error log messages to the code example you shared. Please add error logs as shown here and register a new user to trigger this code. Then check the error log file and include any messages you find there.

add_action( 'register_new_user', 'create_portfolio_post', 10, 1 );

function create_portfolio_post( $user_id )
{
error_log('create_portfolio_post called with user ID: ' . $user_id);
// Get user info
$user_info = array();
$user_info = get_userdata( $user_id );
// $user_roles = $user_info->roles;

// New code added
// $this_user_role = implode(', ', $user_roles );
//if ($this_user_role == 'subscriber')
//{

// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_author' => $user_id,
'post_content' => $user_info->nickname,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'portfolio' // <- change to your cpt
);
// Insert the post into the database
wp_insert_post( $user_post );
error_log('wp_insert_post called with arguments: ');
error_log(print_r($user_post, true));
//}
#1895749

Let's disregard all the irrelevant notices in the logs for now. Obviously those should be cleaned up, but that's a matter for another ticket. All I'm interested in right now is the information I added using error_log() statements in the code example. If you added the error log statements I showed in my previous example, the log should contain a message like this after user registration:

create_portfolio_post called with user ID: 12345

Instead of 12345, it should include the new User's ID.

If the log file does not contain this information, then one of two things is happening:
1. The code you added is not being executed for some reason. Did you add the code in a child theme's functions.php file, or in Toolset > Settings > Custom Code, or in a 3rd-party code snippets plugin? If it's in a child theme's functions.php file, be sure the file on the server is up-to-date. If you're editing locally, be sure to upload the file to the correct spot on the server. If you added the code in a Toolset Custom Code snippet, check to be sure the snippet is active and set to run everywhere. If you added the code using a 3rd-party snippet system, you'll need to consult that system's support team or documentation to be sure it is working correctly.
2. The register_new_user hook is not being fired for some reason I do not understand when a new user registers. You could try using the cred_save_data hook instead if the registration happens with a Toolset Form. I can help troubleshoot that hook since it's a Toolset API.

#1895779
custom code.png

Chris,

Thank you for working through on this one with me. The log does not have the data you mentioned. The code is in the Toolset custom code section and is active and runs always and everywhere.

I am open to trying the toolset form to register but before we do that I can give you admin access and you can

How does that sound?

#1895803

I am open to trying the toolset form to register but before we do that I can give you admin access and you can

How does that sound?

I missed part of that, but if you provide admin access and a bit more information about what you'd like for me to test, I'll be glad to take a look and give you an update when my shift begins again in a few hours.

#1896409

The code snippet seems to be set up correctly and active everywhere. The Portfolio post type does not seem to contain any obvious setup issues. I don't see anything obviously wrong in the Subscriber role settings in Access control, but "right" and "wrong" are subjective here.

I'm still not sure why the "register_new_user" hook isn't being fired, but I'm not really an expert with that hook or other 3rd-party registration processes. I edited the code snippet and changed it to hook into user_register instead, as a test. Now when I create a new User in wp-admin, the Portfolio post is generated automatically as expected. You can see mine here:
hidden link

So I suspect the problem is that the register_new_user hook isn't fired during whatever registration process you are using to register new Users. I'm not sure why the error logs aren't being written out...perhaps logging has been disabled in wp-config.php, or the output has been moved to a different location? If you'd like to give FTP access I can take a look at your wp-config.php file.

#1896419

Yes error logging is turned off in your wp-config.php file:

define('WP_DEBUG', False);

That's why you're not seeing error log messages written to the error log file. To enable logs correctly, change it to:

define('WP_DEBUG', true);

Then add these lines, just after the WP_DEBUG line:

define('WP_DEBUG_LOG', dirname(__FILE__) . '/error_log.txt');
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

Then logs will be written to the /error_log.txt file as expected.

#1896425

Chris,

The log was turned on until last night when I changed the log back to the normal wp-config.php as I did not want the error log to keep on expanding.

The one I shared with you earlier was downloaded after enabling error log.