Sauter la navigation

[Résolu] Fetch a value from generic field and store it on custom field

This support ticket is created Il y a 5 années et 1 mois. 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)

Ce sujet contient 1 réponse, a 2 voix.

Dernière mise à jour par Jamal Il y a 5 années et 1 mois.

Assisté par: Jamal.

Auteur
Publications
#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 lien caché
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 lien caché 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":"lien caché","label":"<img src='lien caché' />"},
{"value":"lien caché","label":"<img src='lien caché' />"},
{"value":"lien caché","label":"<img src='lien caché' />"},
{"value":"lien caché","label":"<img src='lien caché' />"},
{"value":"lien caché","label":"<img src='lien caché' />"},
{"value":"lien caché","label":"<img src='lien caché' />"},
{"value":"lien caché","label":"<img src='lien caché' />"},
{"value":"lien caché","label":"<img src='lien caché' />"}
]
}
[/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? lien caché

#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.