I have custom code code called "featuredimage" as follows :
toolset_snippet_security_check() or die('Direct access is not allowed');
add_action('cred_save_data', 'set_image_as_featured', 10, 2);
function set_image_as_featured($post_id, $form_data)
{
// if a specific form
if ($form_data['id'] == 4856 or 610) {
if (isset($_POST['wpcf-gallery'])) {
$image = $_POST['wpcf-gallery'][0];
// Save the first image as featured image
set_post_thumbnail($post_id, attachment_url_to_postid($image));
}
$people_gen_field = $_POST['person']["PL"]; //get value of 'person' field in array
$related_id = $_POST['relatedperson']['RLPL'];
$delperson = array_values(array_diff($related_id, $people_gen_field));
$addperson = array_values(array_diff($people_gen_field, $related_id));
$arrlength = count($people_gen_field);
$addpersonlength = count($addperson);
$arrrelatedlength = count($delperson);
$arrrelid = count($related_id);
disconnect_posts($delperson, $arrrelatedlength, $post_id);
disconnect_all_post($people_gen_field,$related_id, $arrrelid, $post_id);
connect_posts($addperson, $addpersonlength, $post_id);
connect_posts($people_gen_field, $arrlength, $post_id);
$location_gen_field = $_POST['location']["LC"]; //get value of 'location' field in array
$arrlength = count($location_gen_field);
if (!empty($location_gen_field)) {
$x = 0;
do {
// Connect it in a relationship between journal post and person
toolset_connect_posts('journal-location', $post_id, $location_gen_field[$x]);
$x++;
} while ($x < $arrlength);
//var_dump ($location_gen_field);
}
}
}
function disconnect_posts($field, $legth, $post_id){
if (!empty($field)){
$x = 0;
do {
toolset_disconnect_posts('journal-people', $post_id, $field[$x]);
$x++;
} while ($x < $legth);
}
}
function connect_posts($field, $legth, $post_id){
if (!empty($field)){
$x = 0;
do {
toolset_connect_posts('journal-people', $post_id, $field[$x]);
$x++;
} while ($x < $legth);
}
}
function disconnect_all_post($field, $field1,$legth, $post_id){
if ($field == NULL){
$x = 0;
do {
toolset_disconnect_posts('journal-people', $post_id, $field1[$x]);
$x++;
} while ($x < $legth);
}
}
My question is why this custom code only work if I use it on "Custom code snippets" of toolset. Is there any way to make this code work with snippet?