Skip Navigation

[Resolved] Unable to use $post_id as a variable in cred_save_data to update field

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 1 reply, has 2 voices.

Last updated by Minesh 1 year, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2477399

I am rendering a toolset form using url param

$job_id = $_GET['job-id'];

if($job_id != null || $job_id != 0) {
$output .= '[cred_form form="';
    $output .= 'form-id" post="';
	$output .= 	$job_id;
	$output .= '"]';
	} 

The form loads just fine and work well except for updating one custom field while running code on cred_save_data hook. I am having this strange issue where I can use the $post_id to get the data but when I pass it as a variable to update toolset custom field nothing happens. The field updates when I hard code the value. The code:

add_action('cred_save_data', 'name',20,2);
function name($post_id, $form_data){
  // if a specific form
    if ($form_data['id']==form-id) {  
       $job_id = $post_id;
     // will use company name to create usernames for each user
      $company_name = get_post_meta($post_id, 'wpcf-company', true);
/     $company_name_no_space = str_replace(' ', '', $company_name); // can't have space in username
// resume field will have multiple values.
     $resume_array = get_post_meta($post_id, 'wpcf-resume-job', false);
      foreach( $resume_array as $RESUME_URL) {
      # INPUT - RESUME URL
	# Get the data from the RESUME URL
	#echo("Fetching the Resume data from the Resume URL");
	#$resume_api_call = curl_init($RESUME_URL);
	#curl_setopt($resume_api_call, CURLOPT_RETURNTRANSFER, true);
	#$resume_data = curl_exec($resume_api_call);
	#curl_close($resume_api_call);
	#echo("\nResume fetched successfully, now uploading Resume );

	# Sending the data to the  API for JSON conversion
	$API_KEY = "KEY";
// API code -- removed for privacy
  //check if JSON is not null
  if( $resume_json != null) {
	$json_data = json_decode($resume_json,true);
    
    //extract the data to create a user
    $user_first_name = $json_data['data']['name']['first'];
    $user_last_name = $json_data['data']['name']['last'];
    $user_first_email = $json_data['data']['emails'][0];
    $user_country_code = $json_data['data']['location']['countryCode'];
    $user_linkedin = $json_data['data']['linkedin'];
    //other role related fields to add from job to the user so that processing happens 
    // target role, target seniority level, job search status, resume
    $job_target_role = get_post_meta($post_id, 'wpcf-target-role-job', true);
     $job_seniority_terms = get_the_terms( $post_id, 'job-seniority-level' );
            if(!empty($job_seniority_terms)) {
                foreach ( $job_seniority_terms as $job_seniority_term ) {
                    $job_seniority_term_slug = $job_seniority_term->slug;
                }
            }
    
    //Create user 
    //create username in the format google-david
$user_name = $company_name_no_space . '-' . strtok($user_first_email, '@');
    $user_email = $company_name_no_space . '.' . $user_first_email;  
    $display_name = $user_first_name . ' ' . $user_last_name;
   $result = wp_insert_user( array(
  'user_login' => $user_name,
  'user_pass' => 'prowess',
  'user_email' => $user_email,
  'first_name' => $user_first_name,
  'last_name' => $user_last_name,
  'display_name' => $display_name,
  'role' => 'subscriber'
));  /// user is created
    if(!is_wp_error($result)){
    $user_id = $result;
      // save all data in user meta for future use
      update_user_meta($user_id, 'wpcf-resume-all-data', $resume_json);
      //if resume is shared, it means person is looking for a job
      update_user_meta($user_id, 'wpcf-job-search-status', 1); // 1 = Actively looking 

      
  //    update_user_meta($user_id, 'wpcf-target-role', $job_target_role);
      
      //Add the portfolio post for the user.
      //check if portfolio post already exists
  $args = array(
    'author'        =>  $user_id,
    'post_type' => 'portfolio',
    'post_status' => 'draft,publish,private'
    );
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ){
}
  else {
 
$portfolio_owner_email = $user_first_email;
   
// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_author' => $user_id,
'post_content' => $user_info->nickname,
'post_status' => 'draft', // <- here is to mark as draft. Use should make it public when they want
'post_type' => 'slug' // <- change to your cpt
);
// Insert the post into the database
if ($user_id > 0) {  
    $new_post_id = wp_insert_post( $user_post ); // post is created
  if(!is_wp_error($new_post_id)) {
    update_post_meta( $new_post_id,  'wpcf-portfolio-owner-email', $portfolio_owner_email ); // update works
    $job_id = $_GET['job-id'];
  update_post_meta( $new_post_id, 'wpcf-talent-screen-job-id', $job_id ); // update does not work
//  add_post_meta( $new_post_id, 'wpcf-talent-screen-job-ids', $post_id, true); 
  $portfolio_post_link = get_post_permalink($new_post_id);
  update_user_meta($user_id, 'wpcf-portfolio-link', $portfolio_post_link); // update works
  }
} // if user id is not null
  }
} // if no error in creatin user
} // resume json is not null
} // loop over resumes
} // right form
} // function close

In this code, I am able to update all CPT and user fields above except talent-screen-job-id (toolset field setup - hidden link).
This is a real field. I have double checked it. I am able to pass value in it if $job_id is hardcoded but not otherwise. As $job_id comes from $post_id in CRED form, which is supposed to be a available across the code. Why is this not updating? What am I missing?

Is the $post_id not consistent across the processing of cred_save_data? What could I be missing?

#2477547

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

The code you shared is pure custom code and its hard to tell without checking whats going wrong with your setup.

What if you try to use the Toolset Form hook "cred_submit_complete" instead of "cred_save_data":
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

Do you see any difference?

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