Skip Navigation

[Resolved] Custom post not assigned to User (Author) automatically

This support ticket is created 4 years, 11 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by FelipeP5703 4 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1232951

I am trying to: hidden link

#1233278

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Felipe,

Thanks for asking! I'd be happy to help.

When a new post is added through a front-end form, the currently logged-in user who has submitted the form is set as its author.

In case of a visitor (who is not logged in), no post author is set, just as you noted in your video.
( at 1:53 )

From your video, it is not clear exactly what will be the flow of user and post creation through these forms. But, the way I see it, there are two possibilities:

Case 1: Where the actual user will himself/herself fill these two forms:

If the actual user will be submitting both these forms on your website, it would be best to offer the second form to add a new post, only once he/she has logged in. As a result, he/she will be added as the author of any posts that he/she will add in the logged in state.

Case 2: If another user will be submitting these forms, on behalf of other users:

In this case, you'll need to add some custom code.

First, you'll need a function hooked to "cred_success_redirect" ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect ), which will get the newly created user's ID, when the first form will be submitted and add it to the redirected URL's "user_id" parameter:


// set created user's ID in the URL parameter "user_id"
add_filter('cred_success_redirect', 'custom_user_author_redirect',10,3);
function custom_user_author_redirect($url, $post_id, $form_data) {
	if ($form_data['id']==1234) {
		$redirect = $url . "?user_id=" . $post_id;

		return $redirect;
	}
	return $url;
}

Note: Replace "1234" with the actual ID of your user form.

Next, you'll add a hidden generic field in your second form for the post, which will get the value of this user's ID, from the URL parameter:
( ref: https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field )


[cred_generic_field type='hidden' field='user_id' urlparam="user_id"]
{
"default":""
}
[/cred_generic_field]

In the last step, you'll need a function that will hook to "cred_save_data" ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data ), and will update the author of the newly created post, when the second form will be submitted:
( ref: https://codex.wordpress.org/Function_Reference/wp_update_post )


// change author of post when the form submits
add_action( 'cred_save_data', 'custom_user_author_update', 10, 2 );
function custom_user_author_update( $post_id, $form_data ) {
	if ($form_data['id']==1234) {

		$post = array(
			'ID' => $post_id,
			'post_author' => $_POST['user_id']
		);

		wp_update_post( $post );
	}
}

Note: Replace "1234" with the actual ID of your post form.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1233310

Waqar, first of all, thank you for the detailed explanations.

Case 1: Where the actual user will himself/herself fill these two forms:

If the actual user will be submitting both these forms on your website, it would be best to offer the second form to add a new post, only once he/she has logged in. As a result, he/she will be added as the author of any posts that he/she will add in the logged in state.

This is the case I want.

I thought that by creating a User form, then redirecting the new user to the Post form would automatically do that.

My understanding was that after the user puts his/her info (name and email) into the User Form, the User form would create a new user and automatically have him/her logged in. Is that possible?

I do not want to create unnecessary extra steps for the User if possible.

The workflow I want is the following:
- New User puts his info (name and email) into the User Form (hopefully have him/her already logged in)
- Then it's redirected to the Post Form

Thanks in advance!

#1233730

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Felipe,

Thanks for writing back.

The user form doesn't automatically sign-in the user, after the registration but you can include some custom code for this.

Following forum replies have more information on the topic:

https://toolset.com/forums/topic/automatically-log-in-user-after-registration/#post-629044
https://toolset.com/forums/topic/automatically-log-in-user-after-created/

regards,
Waqar

#1233801

I showed it on this video that the code is not working: hidden link

In the video, I showed that I have implemented the code correctly in the function.php file and changed the ID to the correct form ID.

Also showed that the page where the user is redirected to is in Post Group Access Control, therefore, only logged in users can access it, but the code is not working.

Not sure what to do now

#1234129

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Felipe,

Thanks for the update.

From your recent screencast, I noticed two things:

1. Please use the code from Nigel, that he suggested in this reply as it works in my tests:
https://toolset.com/forums/topic/automatically-log-in-user-after-registration/#post-629044
( of course, you'll replace the form's ID )

2. For this code snippet to work, it is important that the user's password is not automatically generated and its field is also available in the user form:


<div class="form-group">
<label>Password</label>
[cred_field field='user_pass' class='form-control' output='bootstrap']
[cred_field field='user_pass2' class='form-control' output='bootstrap']
</div>

Once, the password is available in the submitted form's data, only then that code snippet will be able to automatically log in the user.

regards,
Waqar

#1234415

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.