Skip Navigation

[Resolved] Prompt user to register/login before submitting child post

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

Problem:
The client has a set-up where, from a parent post, a user can add a child post via a link to a front-end Toolset Form.

The particular requirements for this project are that only a logged-in user should be able to see and submit the child post form. Guest users should see a login/register form instead, and then continue to see and submit the child post form.

Solution:
There are two problems here.

The first is that guest users who register are not automatically logged in, they would register and be redirected to a log-in page, and then the flow would be interrupted.

The second is that, even if we add code to automatically log in users who register and then present them with the child post form, the URL parameter that identifies the parent is lost.

The thread below describes what is required and provides suggestions for the custom code that is needed for this to work: https://toolset.com/forums/topic/prompt-user-to-register-login-before-posting-content/#post-1188864

This support ticket is created 5 years, 3 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by deanL 5 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#1188015

On this page: hidden link — when users click on "Share your story" button, they will be redirected to the page that contains post form for creating child post. So far so good!

Now what do I do if I want the page to show register/login form if the user is not already logged in. Then, upon successful registering/logging in, redirect user back to the post form again to process to create post?

I figure I may also perform the check right from the parent post itself, ie if user is not already logged in, display register/login button; otherwise, display "Share your story" button.

Either way is fine for me, but I wonder if Toolset has any built in function for performing the check?

#1188113

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Dean

Let me give you some pointers as to what to do, and if you get stuck let me know and I'll set up a test site with the same and clarify exactly what is required for it to work.

Leave your current set-up intact, so that any user can click the link to share their story, which takes them to the page with the form to publish the child post.

Now, make sure you have the Access plugin installed and active.

With Access installed, you can specify who is able to use particular forms, and what should be displayed for those who cannot:
https://toolset.com/documentation/user-guides/access-control-for-cred-forms/

However, in your case I think we want something similar-but-different: https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/

Currently your page will have a shortcode to insert the form. You should wrap that in an access shortcode to only show the content where the current user is logged in.

Then add another access shortcode for users who are not logged in, and within that add the shortcodes for a log-in form or a registration form.

If you include such forms on the same page and have the page reload the user will now be logged in and so should see the form to publish the child post.

Try that, or if you want a workflow that is different, let me know.

#1188800

Hello Nigel, thank you for looking into this.

I managed to add in the register and login form visible to guest users. And the post form for logged in users.

However, when I tried to register, the form did not log me in directly and reload to show the post form. Because of that, I was returned to the registration form again. What am I missing?

#1188864

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, I set up a test site to do the same, and the steps are demanding, requiring some custom code.

To recap, when you have a parent post that links to a form to publish child posts, the link includes a URL parameter that specifies the originating parent, so that the child post will know to which parent it belongs.

There are a couple of problems with the registration part of this.

One is that there is no built-in means for the user that registers with a user form to be logged-in automatically, they would need to register, then to log in.

And after the registration form has been submitted, if it is set to redirect to the page with the form to add a child post (the original objective) then the URL parameter that identifies the parent that the child belongs to is lost.

So, I propose to bits of code that you can add in a single custom code snippet (at Toolset > Settings > Custom Code):

/**
 * Auto-login new CRED user
 */
add_action('cred_save_data', 'tssupp_cred_autologin', 10, 2);
function tssupp_cred_autologin($post_id, $form_data) {

	if (168 == $form_data['id']) { // Edit as required

		if (!empty($_POST['user_login']) && !empty($_POST['user_pass'])) {

			// get the user credentials from the $_POST object
			$user = array(
				'user_login' => $_POST['user_login'],
				'user_password' => $_POST['user_pass'],
				'remember' => true,
			);
			$login = wp_signon($user, false);

			if (is_wp_error($login)) {
				error_log($login->get_error_message());
			}

		}
	}
}

/**
 * Include parent post URL parameter when redirecting
*/
add_filter('cred_success_redirect', 'tssupp_custom_redirect', 10, 3);
function tssupp_custom_redirect($url, $post_id, $form_data) {

	if (168 == $form_data['id']) { // Edit as required

		$url = $_SERVER['HTTP_REFERER'];
	}

	return $url;
}

The first part is to automatically log in a new user that registers with a user registration form. The second part adds back the URL parameter that identifies the parent when redirecting to the page with the child form. Note that in two places you need to edit the ID of the user form for registering users (168 in my code sample).

To be clear, the page where I insert the form to publish child posts looks like this on my test site:

[toolset_access role="Guest" operator="deny"]
[cred-form form="submit-task" ]
[/toolset_access]

[toolset_access role="Guest" operator="allow"]
[wpv-login-form allow_remember="true"]

--OR--

[cred_user_form form="register"]
[/toolset_access]

Logged-in users see the form directly, logged-out users see the log-in form *and* the registration form (inserted directly on the same page, not linked to elsewhere).

The settings of the registration form are to redirect back to this same page (the page that hosts the form to publish child posts), with no delay.

Good luck!

#1188940

Nigel, I'm pretty sure you get this a lot. That people telling you that you are amazing. Because YOU ARE!

Your solution has solved my biggest problem! I'm finally close to the completion of this project! I can't thank you more! No seriously, please tell me how else can I thank you!

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