Skip Navigation

[Resolved] Fetch a value from generic field and store it on custom field

This support ticket is created 4 years, 7 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Jamal 4 years, 7 months ago.

Assisted by: Jamal.

Author
Posts
#1608385

Tell us what you are trying to do?
I'm trying to fetch a value from a generic field to store in custom post field "student-photo"
here is the screenshot of what I'm trying to do hidden link
This is used for student photo that will serve as their Profile photo if they don't have image uploaded yet.
You need to signup first as a student then you will redirected to next registration form hidden link to select or upload photo.

Here is the generic field code we are using:
[cred_generic_field field='preset-student-profile-image' type='radio' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"persist":1,
"options":[
{"value":"hidden link","label":"<img src='hidden link' />"},
{"value":"hidden link","label":"<img src='hidden link' />"},
{"value":"hidden link","label":"<img src='hidden link' />"},
{"value":"hidden link","label":"<img src='hidden link' />"},
{"value":"hidden link","label":"<img src='hidden link' />"},
{"value":"hidden link","label":"<img src='hidden link' />"},
{"value":"hidden link","label":"<img src='hidden link' />"},
{"value":"hidden link","label":"<img src='hidden link' />"}
]
}
[/cred_generic_field]

I'm trying this code in functions.php
//Let the student user choose an image for their profile
add_action('cred_save_data', 'my_save_student_image',10,2);
function my_save_student_image($post_id, $form_data)
{
if ($form_data['id']==19085)
{
// Create post object
$new_post_id = wp_insert_post( array(
'post_title' => $_POST['post_title'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $post_ID,
'post_type' => 'student'
) );

// get image URL value from the user
$user_image = get_post_meta( $post_id, 'wpcf-preset-student-profile-image', true );
// if image URL exists
if (!empty($user_image)) {
// set the image URL as new post's custom field value
update_post_meta($post_id, 'wpcf-student-photo', $user_image);
// delete the image URL from user's custom field
delete_user_meta($post_id, 'wpcf-preset-student-profile-image');
}
}
}
But seems it will add another student post.
What is the link to your site? hidden link

#1608865

Hello and thank you for contacting the Toolset support.

This code will, of course, create another student post as it uses the function wp_insert_post. You will have to remove this code:

// Create post object
$new_post_id = wp_insert_post( array(
'post_title' => $_POST['post_title'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $post_ID,
'post_type' => 'student'
) );

I hope this helps. Let me know if you have any questions.