Skip Navigation

[Resuelto] I’m stuck on 2 things

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

I've created a "Create New User" user form.

When this form is submitted, it redirects to a page with the "Create New Profile" form.

So I'm stuck on 2 things..

1) I'd like to pass on the Firstname, Lastname and Email from the newly created WP User profile, to pre-fill equivalent fields on the profile.

2) I'd like the "Create New Profile" firm to have a dropdown to pick the user for whom it's creating a profile.

Solution:

There aren't such kinds of features(Both 1 and 2), it needs custom codes, for example:

https://toolset.com/forums/topic/im-stuck-on-2-things/#post-1290227

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field

This support ticket is created hace 5 años, 4 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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/Hong_Kong (GMT+08:00)

Este tema contiene 4 respuestas, tiene 2 mensajes.

Última actualización por Gerard hace 5 años, 4 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#1290127

Ok, I'm now trying to implement what we've discussed here.. So far so good.

So I've created a "Create New User" user form.

When this form is submitted, it redirects to a page with the "Create New Profile" form.

So I'm stuck on 2 things..

1) I'd like to pass on the Firstname, Lastname and Email from the newly created WP User profile, to pre-fill equivalent fields on the profile.

2) I'd like the "Create New Profile" firm to have a dropdown to pick the user for whom it's creating a profile.

Thanks

#1290227

Hello,

There aren't such kinds of features(Both 1 and 2), it needs custom codes, here are my suggestions:

1) After submit the user form for creating new user, you can use filter hook "cred_success_redirect" to trigger a custom PHP function, pass new user's ID value as URL parameter "new-user-id" to target page, for example:


add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $user_id, $form_data)
{
    if ($form_data['id']==123)
       $url .= '?new-user-id=' . $user_id;
   
    return $url;
}

Please replace 123 with your user form's ID.

Above codes will change the redirection URL as below:
enlace oculto

See document
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

2) In the "Create New Profile" form's content, you can use a hidden generic field to capture the new user's ID, for example:

[cred_generic_field type='hidden' field='new-user-id' urlparam="new-user-id"]
{
"default":"",
"generic_type":"user_id",
"persist":1
}
[/cred_generic_field]

See document:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field

3) After user submit the "Create New Profile" form, above hidden field will be able to pass POST parameter "new-user-id" to Toolset form, you can use action hook cred_save_data to trigger a custom PHP function, in this function setup the new "Profile" post's author as the new user's ID, for example:

add_action('cred_save_data', 'my_save_data_action', 999, 2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==789)
    {
        if (isset($_POST['new-user-id']))
        {
			$my_post = array(
				'ID'           => $post_id,
				'post_author'   => $_POST['new-user-id'],
			);
			wp_update_post( $my_post );
        }
    }
}

Please replace 789 with your "Create New Profile" form's ID.

More help:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_update_post

#1291333

Thank you!!

I'm now trying to also pass the user form's firstname, lastname, and email to save to the proper fields in the profile CPT.

In the past I'd used cred_save_data, so trying this:

/* Pass the form ID to the New Student Profile Form  */
add_filter('cred_success_redirect', 'gg_custom_redirect',10,3);
function gg_custom_redirect($url, $user_id, $form_data)
{
    if ($form_data['id']==202) {
       $url .= '?new-user-id=' . $user_id;
       $url .= '?first-name=' . $first_name;
       $url .= '?last-name=' . $last_name;
       $url .= '?user-email=' . $email;
		
	return $url;
	}
}


add_action('cred_save_data', 'my_save_data_action', 999, 2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==212)
    {
        if (isset($_POST['new-user-id']))
        {
            $my_post = array(
                'ID'            => $post_id,
                'post_author'   => $_POST['new-user-id'],
 				'post_title'    => $_POST['user-email'],
            );
			
//            wp_update_post( $my_post );
        }

		if (isset($_POST['first-name'])) {
			$fname = $_POST['first-name'];
			update_post_meta( $post_id, 'wpcf-first-name', $fname );
		}

		if (isset($_POST['last-name'])) {
			$lname = $_POST['last-name'];
			update_post_meta( $post_id, 'wpcf-last-name', $lname );			
		}
		
		if (isset($_POST['user-email'])) {
			$email = $_POST['user-email'];
			update_post_meta( $post_id, 'wpcf-personal-email', $email );
		}

		wp_update_post( $my_post );
	}
}

but it looks like I'm not getting the fields values passed..

I did add these to the Profile form:

[cred_generic_field type='hidden' field='first-name' urlparam="first-name"]
{
	"required":1,
	"generic_type" : "textfield",
	"default":"abcde12345",
}
[/cred_generic_field]
      
[cred_generic_field type='hidden' field='last-name' urlparam="last-name"]
{
	"required":1,
	"generic_type" : "textfield",
	"default":"abcde12345",
}
[/cred_generic_field]
      
[cred_generic_field type='hidden' field='user-email' urlparam="user-email"]
{
	"required":0,
	"validate_format":0,
	"generic_type" : "email",
	"default":""
}
[/cred_generic_field]

Just hacking away..

Thanks again for your excellent support 🙂

#1291357

You can use user's ID to get other user information, for example, modify your PHP codes as below:

...
function gg_custom_redirect($url, $user_id, $form_data)
{
    if ($form_data['id']==202) {
        $user_info = get_userdata($user_id);
        $first_name = $user_info->first_name;
        $last_name= $user_info->last_name;
        $email = $user_info->user_email;
...

More help:
https://codex.wordpress.org/Function_Reference/get_userdata

#1304525

Thank you very much for all your help Luo Yang.. 🙂