Skip Navigation

[Resolved] Request login/registration before form submission

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

Problem: I have a form that allows Guests to submit a new post. I would like to show a modal that requires registration before the new post can be submitted.

Solution: Since there is no public JavaScript API for Forms, I don't have any examples to share for displaying a modal and requiring registration before submission. Instead, you could create two separate forms on two separate pages. Here are some more details and some sample code you can check:
- Create the first form, which should create new posts of some type in draft state
- Add the form to a custom Page
- Create another custom Page that will eventually contain the second form
- Set the form to redirect to a custom page that will contain the second form
- Add some custom PHP code using the CRED API that appends the new post ID to the redirect URL:

add_filter('cred_success_redirect_1234', 'custom_redirect_for_form_with_id_1234', 10, 3);
function custom_redirect_for_form_with_id_1234( $url, $post_id, $form_data )
{
    $newurl = $url . "?postid=" . $post_id;
    return $newurl;
}

- This means the redirect will now include the new post ID in the URL as a parameter. We will use this to assign the new post to the new User later.
- Create the second form, which will create new Users
- Add a generic field and set the value of the generic field to be the same as the URL parameter "postid" using the wpv-search-term or wpv-post-param shortcode.
- Insert the second form in the correct custom Page
- Add some custom PHP code using the CRED API that modifies the author of the new post when the new User is created, using the generic field value. Something like this:

add_action('cred_save_data_2345', 'new_user_author_action_2345',10,2);
function new_user_author_action_2345($user_id, $form_data)
{    
      // Create post object
      $args = array(
       'ID' => $_POST['your-generic-field-slug'],
       'post_author'   => $user_id
      );
 
      // change the post author
      wp_update_post( $args );
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_update_post

This support ticket is created 6 years, 8 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)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#906891

Hello there,
I'd like to know if the following is possible.

I noticed that many users are scared away when they are asked to sign up before filling out a form. They would like to start filling out the form right away.

So what I'd like to do is have a user fill out a form without needed to register or log in, but before he submits it he's asked to sign up or log in.

Do you know what I mean? How's that possible?

So i.e.: Logged out user clicks on submit button --> Popup: Register now to publish/save your post.

Regards,
Nick

#907138

There's not an easy way to do this in Toolset, and I'm not sure that it's possible just like you described. First, there is no CRED JavaScript API. This means that there is no way to tap into the submission event, so no way to trigger a modal before submission. With some custom code you might be able to get around that, but then I'm not sure if the post would be associated with the correct author without refreshing the page...which would lose the progress in the post form. It would take some testing because it's kind of an unexpected use of CRED.

To do something similar in the Toolset system, I think you could allow the post form to be submitted, creating the post in a draft state. Then redirect the User to a different page containing a registration form. Then if they continue to register, you would have to programmatically associate the post that was just created (the author is "Guest") with the new User (the correct author) using the cred_save_data API. If not, the draft post remains a Guest draft post. If you'd like some more information about how that could work, let me know.

#907141

Hello Christian.
Yes, I'd appreciate more information about your suggested method.
I think that could work in my case.

#907162

Here are some more details and some sample code you can check:
- Create the first form, which should create new posts of some type in draft state
- Add the form to a custom Page
- Create another custom Page that will eventually contain the second form
- Set the form to redirect to a custom page that will contain the second form
- Add some custom PHP code using the CRED API that appends the new post ID to the redirect URL:

add_filter('cred_success_redirect_1234', 'custom_redirect_for_form_with_id_1234', 10, 3);
function custom_redirect_for_form_with_id_1234( $url, $post_id, $form_data )
{
    $newurl = $url . "?postid=" . $post_id;
    return $newurl;
}

- This means the redirect will now include the new post ID in the URL as a parameter. We will use this to assign the new post to the new User later.
- Create the second form, which will create new Users
- Add a generic field and set the value of the generic field to be the same as the URL parameter "postid" using the wpv-search-term or wpv-post-param shortcode.
- Insert the second form in the correct custom Page
- Add some custom PHP code using the CRED API that modifies the author of the new post when the new User is created, using the generic field value. Something like this:

add_action('cred_save_data_2345', 'new_user_author_action_2345',10,2);
function new_user_author_action_2345($user_id, $form_data)
{    
      // Create post object
      $args = array(
       'ID' => $_POST['your-generic-field-slug'],
       'post_author'   => $user_id
      );

      // change the post author
      wp_update_post( $args );
}

API details here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_update_post