Skip Navigation

[Resolved] More than one Cred ID in callback function

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

Problem: I am using the cred_save_data hook to update the status of a post, but I am having trouble adding a conditional to restrict the callback to more than one Form ID.

Solution: Check your callback syntax. You can create an array of Form IDs, then test the current Form ID against that array. You must include the $form_data parameter as an input to your callback function to access the current Form's ID.

// CRED PUBLISH STATUS
add_action('cred_save_data', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id, $form_data) {
 
   
  if($_REQUEST['post_status'] == ''){
  //Do nothing
  return;
  }
   
  //create an array of values with all ID's of the Forms you want to check:
  $ids = array("101","102","103");
    
  //Check if the current form ID is one of in the above array:
  if (in_array($form_data['id'], $ids) ){
   
  $my_post['ID'] = $post_id;
  $my_post['post_status'] = $_REQUEST['post_status'];
  // Update the post into the database
  wp_update_post( $my_post );
  }
}

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

This support ticket is created 6 years, 4 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 8 replies, has 3 voices.

Last updated by Nashaat 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#954736

how can i add more than one cred form id in function? i have 3 Cred forms and would like to assing the function for all of them

this is my code

// CRED PUBLISH STATUS
add_action('cred_save_data_103', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id) {
$my_post['post_status'] = $_REQUEST['post_status'];
// Update the post into the database
wp_update_post( $my_post );
}

i would like to add following ids 101 , 103

#955366

This only requires to modify the PHP script rule of the if statement, it is not related to the Toolset Forms API.

In PHP, you can pass several conditions to a IF statement.
hidden link

When you consult our Documentation about the API we show that you can pass the ID of the Form to the Forms API hook with a variable ($form_data['id']), or as you do it, directly in the functions name.

The approach you take is documented as:

//Custom success action can also be performed for a specific form by using the form ID:

It is just for one form.

If you want to address many forms, you need to use the if statement as shown on the other example:

if ($form_data['id']==12)

Here, if you want to use many forms, you can just update that IF to accept several true values:

//create an array of values with all ID's of the Forms you want to check:
$ids = array("12","23","33");

//Check if the current form ID is one of in the above array:
if (in_array($form_data['id'], $ids) )

That, will allow you running the code on forms with ID "12","23","33"

Let me know if you need more details!

#955484

I have followed your instructions and this works fine now with changing the post status within cred form.

with following function

// CRED PUBLISH STATUS
add_action('cred_save_data', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id) {
  
  //create an array of values with all ID's of the Forms you want to check:
  $ids = array("101","102","103");

  //Check if the current form ID is one of in the above array:
  if ( ( $form_data['id'] == 101 ) OR ($form_data['id'] == 102) OR ($form_data['id'] == 103) );
  
  $my_post['post_status'] = $_REQUEST['post_status'];
  // Update the post into the database
  wp_update_post( $my_post );
}

although an issue just popped up when using above mention code. I am using another function to get Post Title from Taxonomies and custom Fields

// POST TITLE FROM TAXONOMIES & CUSTOM FIELDS
add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data) {
  
	 if ( ( $form_data['id'] == 103 ) OR ($form_data['id'] == 104) OR ($form_data['id'] == 101) OR ($form_data['id'] == 105) OR ($form_data['id'] == 102) OR ($form_data['id'] == 106) ){
 
$taxCategory1 = get_the_terms( $post_id, 'car-brand' );
$taxCategory2 = get_the_terms( $post_id, 'car-model' );
$taxCategory3 = get_the_terms( $post_id, 'car-version' );
$field4 = get_post_meta($post_id, 'wpcf-model-year', true);
   
  
    $post_title = $taxCategory1[0]->name . ' ' .$taxCategory2[0]->name . ' ' . $taxCategory3[0]->name . ' ' . $field4; 
$slug = sanitize_title($post_title); 
  
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
}
}

now every time i edit a post with following forms (101, 102, 103) the post title won't change. Only if i delete the "CRED PUBLISH STATUS" code snippet then the "POST TITLE FROM TAXONOMIES & CUSTOM FIELDS" would behave normal again.

I think there is a conflict in the "CRED PUBLISH STATUS" code causing this problem.

#955486
Submit Error.png

I tried following code too but this wont let me submit the form an Error pop up

// CRED PUBLISH STATUS
add_action('cred_save_data', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id) {
  
  if ( ($form_data['id']==101) OR ($form_data['id']==102) OR ($form_data['id']==103) )

  $my_post['post_status'] = $_REQUEST['post_status'];
  // Update the post into the database
  wp_update_post( $my_post );

}
#955488

this code too show same Error when submitting

// CRED PUBLISH STATUS
add_action('cred_save_data', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id) {
  
  //create an array of values with all ID's of the Forms you want to check:
	$ids = array("101","102","103");

	//Check if the current form ID is one of in the above array:
	if (in_array($form_data['id'], $ids) )

  $my_post['post_status'] = $_REQUEST['post_status'];
  // Update the post into the database
  wp_update_post( $my_post );

}

I am sure i am writing something wrong in the functions..

#955550

1. What is $_REQUEST['post_status'] supposed to represent in your code? Is there a generic field in these Forms called "post_status"? Are you trying to access the status of the post created or edited by the Forms?

2. Your "if" statement should probably use curly brackets { } to enclose lines 11, 12, and 13. See the first two examples here for an explanation: http://php.net/manual/en/control-structures.if.php

#955608

1. What is $_REQUEST['post_status'] supposed to represent in your code? Is there a generic field in these Forms called "post_status"? Are you trying to access the status of the post created or edited by the Forms?

I had this from another ticket here : https://toolset.com/forums/topic/change-post-status-draft-publish-of-product-on-the-frontend/#post-920621

I am trying to get the current post status in Cred editing form. see attachment
______

2.

I have tried as your suggestion but couldn't solve this.

// CRED PUBLISH STATUS
add_action('cred_save_data', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id) {
  
  if($_REQUEST['post_status'] == ''){
  //Do nothing
  return;
  }
  
  //create an array of values with all ID's of the Forms you want to check:
  $ids = array("101","102","103");
  
  //Check if the current form ID is one of in the above array:
  if (in_array($form_data['id'], $ids) ){

  $my_post['ID'] = $post_id;
  $my_post['post_status'] = $_REQUEST['post_status'];
  // Update the post into the database
  wp_update_post( $my_post );
  }
}

if i keep the function without any form ID assigned to it like following, it works perfectly with all forms and without any conflicts.

// CRED PUBLISH STATUS
add_action('cred_save_data', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id) {
  
  if($_REQUEST['post_status'] == ''){
  //Do nothing
  return;
  }
  
  $my_post['ID'] = $post_id;
  $my_post['post_status'] = $_REQUEST['post_status'];
  // Update the post into the database
  wp_update_post( $my_post );
  
}

I just want to assign specific Cred IDs to the function but no methods is working...

#955654

Okay I see now, I overlooked something important. You should add another parameter, $form_data, to your callback function. Like this:

function Select_Post_Status_func($post_id, $form_data) {

Without this parameter, the conditional will not work correctly. Try this and let me know if the problem is not resolved.

#955664

YESS you are right the parameter "parameter, $form_data" has solved the issue. THANK YOU

this is my final code

// CRED PUBLISH STATUS
add_action('cred_save_data', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id, $form_data) {

  
  if($_REQUEST['post_status'] == ''){
  //Do nothing
  return;
  }
  
  //create an array of values with all ID's of the Forms you want to check:
  $ids = array("101","102","103");
   
  //Check if the current form ID is one of in the above array:
  if (in_array($form_data['id'], $ids) ){
  
  $my_post['ID'] = $post_id;
  $my_post['post_status'] = $_REQUEST['post_status'];
  // Update the post into the database
  wp_update_post( $my_post );
  }
}

This code is working too now

// CRED PUBLISH STATUS
add_action('cred_save_data', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id, $form_data) {

  
  if($_REQUEST['post_status'] == ''){
  //Do nothing
  return;
  }

  //Check if the current form ID is one of following:
  if ( ($form_data['id']==101) OR ($form_data['id']==102) OR ($form_data['id']==103) ){
  
  $my_post['ID'] = $post_id;
  $my_post['post_status'] = $_REQUEST['post_status'];
  // Update the post into the database
  wp_update_post( $my_post );
  }
}

both codes has the same results the only different is to assign the forms with arrays or just IF condition.