Skip Navigation

[Resolved] How to insert User_id to a custom field ,when the registration form submitted

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

Problem:
How to insert User_id to a custom field ,when the registration form submitted

Solution:
You can use Toolset Form hook "cred_save_data" in order to do any custom processing after the user created.

you can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/how-to-insert-user_id-to-a-custom-field-when-the-registration-form-submitted/page/2/#post-1070111

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years, 5 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 16 replies, has 2 voices.

Last updated by FuChan 6 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#954362

I have a registration form, Userid input phone number.
I have a custom field is "mobile".
I want to update userid to mobile when user registration form submitted.
Is this code correct to what I want?

//After sending out the application form, let "Process Progress" become "Send In".
 
add_action('cred_save_data','func_custom_processing_progress',10,2);
function func_custom_processing_progress($post_id,$form_data) {
    if ($form_data['id']==214) {
           update_post_meta($post_id,'wpcf-processing-progress',1);
           update_post_meta($post_id,'wpcf-date-delivery',today());
    }
}
#954368

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - thank you for creating new ticket. This will help a lot.

As you are dealing with usermeta - you need to use function update usermeta.

add_action('cred_save_data','func_custom_processing_progress',10,2);
function func_custom_processing_progress($post_id,$form_data) {
    if ($form_data['id']==214) {
           update_post_meta($post_id,'wpcf-mobile',$post_id);
   }
}

More info:
https://codex.wordpress.org/Function_Reference/update_user_meta

#955399

Sorry,I re-describe my question:

I have a registration form, The field "Username" let user input phone number.

And I have a custom field is "mobile" in another custom post type "membership mant".

I want to update field "Username" to "mobile" and auto-generate a post of "membership mant" when user registration form submitted.

Can this code achieve my purpose? Thank you.

//After submitted the registration form, auto-generate a member profile, and the username into the mobile field.
add_action('cred_save_data','username_insert_to_mobile',10,2);
function username_insert_to_mobile($post_id,$form_data) {
    if ($form_data['id']==83) {
        $username = get_user_meta($user_id, 'ms_username', true);		
	update_post_meta($post_id, 'wpcf-mobile', $username);
    }
}
#955481

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I still do not understand the relationship between post type "membership mant" and user form.

Do you mean that when a new user register with username where you are allowing user to input mobile number as username, so you want to copy the username (mobile number) to the custom field "mobile" of post type "membership mant" and insert one new entry?

#955531

yes,my mean is this.

thank you.

#956529

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Do you mean that when a new user register with username where you are allowing user to input mobile number as username, so you want to copy the username (mobile number) to the custom field "mobile" of post type "membership mant" and insert one new entry?
==> OK
So, when we insert the new entry for post type "membership mant" what title it should have?

Can I have link to your user registration form so that I can look at the fields.

#957129
201807271514472.png
201807271517404.png

Hi Minesh,

This code is me insert a title for a form of the "memberhsip_mant" post type.

//送出基本信息表單後,自動給標題.
add_action('cred_save_data','membership_mant_title',10,2);
function membership_mant_title($post_id,$form_data) {
    if ($form_data['id']==232) {
        $name = get_post_meta($post_id, 'wpcf-name', true);
        $mobile = get_post_meta($post_id, 'wpcf-mobile', true);
        $title= $name. '-' . $mobile;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
        update_post_meta($post_id,'wpcf-account-available-cash',0);
    }
}

And I attached the screenshot pic. It is the field group of the "membership-mant" post type. Thank you.

#957329

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - so is issue is resolved or you need help here?

#958765

The code inserts a title when I create the new entry of the "memberhsip_mant" post type.(post form id = 232)

But my question is create an entry of the "memberhsip_mant" post type when I submitted a Registration form.(user form id = 83)

My question is not solved. Please help me. Thank you.

#958767

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - please check following code - which is using wp_insert_post() function to insert the post and update the mobile number for that post.

//After submitted the registration form, auto-generate a member profile, and the username into the mobile field.
add_action('cred_save_data','username_insert_to_mobile',10,2);
function username_insert_to_mobile($post_id,$form_data) {
    if ($form_data['id']==83) {
        $username = get_user_meta($user_id, 'ms_username', true);       


$my_post = array(
  'post_title'    => $username,
  'post_content'  => '',
  'post_status'   => 'publish',
  'post_author'   => $post_id,
  'post_type' => 'memberhsip_mant'
);
      
// Insert the post into the database
$insert_id = wp_insert_post( $my_post );
update_post_meta($insert_id, 'wpcf-mobile', $username );
                
    }
}
#958973
201807301728154.jpg
201807301729126.jpg
201807301728555.jpg

Thank you for your help.

I follow your code to set, and I try registration again.

It is succeeded to insert an entry of the "memberhsip_mant" post type, but the title and mobile field are null.

#958977

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to use following code:

add_action('cred_save_data','username_insert_to_mobile',10,2);
function username_insert_to_mobile($post_id,$form_data) {
    if ($form_data['id']==83) {
        $username = get_user_meta($post_id, 'ms_username', true);       
 
 
$my_post = array(
  'post_title'    => $username,
  'post_content'  => '',
  'post_status'   => 'publish',
  'post_author'   => $post_id,
  'post_type' => 'memberhsip_mant'
);
       
// Insert the post into the database
$insert_id = wp_insert_post( $my_post );
update_post_meta($insert_id, 'wpcf-mobile', $username );
                 
    }
}
#959868
201807301740517.jpg
201807301741048.jpg

I updated the code which you provided. But the result is the same.

#959871

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please share problem URL of registration form and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1070111

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - could you please check now, I've added following code to your current theme's functions.php file:

add_action('cred_save_data','username_insert_to_mobile',99,2);
function username_insert_to_mobile($post_id,$form_data) {
    if ($form_data['id']==83) {
				
        $username = $_POST['user_login'];       
  
  
$my_post = array(
  'post_title'    => $username,
  'post_content'  => '',
  'post_status'   => 'publish',
  'post_author'   => $post_id,
  'post_type' => 'membership-mant'
);
        
// Insert the post into the database
$insert_id = wp_insert_post( $my_post );
update_post_meta($insert_id, 'wpcf-mobile', $username );
  	
    }
	
}

You shared wrong post type slug, the correct post type slug is membership-mant