Hello,
I created a user form and added a generic field with the slug 'societe'.
I would like that when the user form is submitted, the information is saved in a custom post type 'auteur' in the respective fields.
how to tell him that the generic user formfield with slug 'societe' must go into the field 'societe' which is in the cpt 'author'
Can they have the same slug?
I checked the cred documentation API https://toolset.com/documentation/programmer-reference/cred-api/
but I can't do what I want.
Here is my code for now :
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==62)
{
$my_post = array(
'post_title' => $_POST['wpcf-societe'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => $post_ID,
'post_type' => 'auteur'
);
wp_insert_post( $my_post );
}
}
Merci d'avance pour votre support.
Olivier Foguenne
Hi, the generic field will not have "wpcf-" prefix in $_POST unless you put "wpcf-" in the generic field slug in the Form builder.
So this code will set the post title to be the value of the societe generic field:
'post_title' => $_POST['societe'],
If you want to set the value of a Types custom field, then you can add it using the 'meta_input' key in your my_post array, like this:
'post_type' => 'auteur',
'meta_input' => array(
'wpcf-field-slug' => $_POST['societe']
)
Hi Christian,
There seems to be a problem in my code, when I remove it, the user adds but when I add the code, nothing happens when I send the form.
The id of the form is the right one,
The slug is good, and the cpt also
The code :
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==62)
{
$my_post = array(
'post_title' => $_POST['societe'],
'post_status' => 'publish',
'post_author' => $post_ID,
'post_type' => 'auteur',
'meta_input' => array(
'wpcf-societe' => $_POST['societe']
)
);
wp_insert_post( $my_post );
}
}
Okay, if no auteur post is created I'll need to log in and take a closer look. In my local tests, this code creates a post automatically and registers a new User. Can you provide login credentials here? Let me know where I can see the Form on the front-end of the site.
Thank you, where can I submit the Form, from the front-end of the site? I can't find it.
You must be disconnected to see the form
It just worked for me, can you check again? New User registered:
hidden link
New Auteur:
hidden link
thanks Christian, it works now...
Is it possible to have a multiple choice generic field with the possibility for the user to encode another proposal if it is not in the list? How can I add such a field? I must then add it to the CPT 'auteur' as a category.
No, there's nothing exactly like that. It would require custom code. The only way to add an option in the Form like that, without custom code, would be to use a taxonomy instead of a custom field. Users can add new terms to a taxonomy, but cannot add new options to a field.
How to use a taxonomy in user form ?
Oh yes, I forgot we were discussing a User Form. So it's not possible to use a taxonomy field here. I guess you could use a generic field, then use Forms email notifications to send a notice to an admin User containing the new value. That admin User would then have to edit the custom field in the User field editor, then edit the User to select the correct value. It would be a very manual process.
Thanks a lot Christian,
I have a other problem with a publication form 'projet'
You can see the form here hidden link
you must be connected with your precedent user 'christian.c' to see the form.
The information is not transmitted but when sending the form disappears.
Okay I have split this question into another ticket so we can discuss in more detail.
Hi Christian,
My user form does not work anymore, nothing happens when I send it.
I modified some elements in my code to retrieve the first and last name and the email in order to send it into the CPT 'auteur'.
Thank you very much for your precious help.
Here's my code:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==62)
{
$my_post = array(
'post_title' => $user_firstname,
'post_status' => 'publish',
'post_author' => $user_ID,
'post_type' => 'auteur',
'meta_input' => array(
'wpcf-type' => $_POST['type'],
'wpcf-prenom' => $user_firstname,
'wpcf-nom' => $user_lastname,
'wpcf-societe' => $_POST['societe'],
'wpcf-telephone' => $_POST['telephone'],
'wpcf-email' => $user_email,
'wpcf-site-web' => $_POST['site-web']
)
);
wp_insert_post( $my_post );
}
}