I have 2 questions, that I'll summarise in a single ticket.
I'm trying to let new users choose their favorite categories (we will be sending emails in function of their choice).
I tryied to add taxonomies in the User Registration form (using CRED), but this does not seem to work. In fact, I need to give the new user the ability to select his favorite Taxonomy to send him later on some emails related to the latter.
The only way I found for now is to add a custom user fields and manually added the taxonomy names. But this is not practical at all, as we will be adding/modifying existing taxonomies in the future.
The second problem is that I want to oblige the user to select at least one taxonomy when he registers (checkboxes).
I don't mind making customisations, but I need your help for that.
Kind regards.
Hi, there's not a built-in way to select a taxonomy and store it in a User's profile like this. The best way to accomplish something like this is to use a generic field and some custom code.
- Create a custom field on the User's profile that stores an ID or slug - this is where you will store the User's favorite taxonomy selection.
- Create a generic field in the signup CRED form. This is where the User will select that taxonomy term. https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
- Create a View of whatever taxonomy you want to include in the User form. Use the technique described here to generate a list of generic field options for each taxonomy term: https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms
- Once the generic field is inserted correctly, your Users will be able to choose from the existing taxonomy terms.
- The last step is to capture their selection from the generic field and add that value to the custom field in their User profile. We offer the CRED API cred_save_data to help with this. I can show you how to set this up when you're ready.
The most difficult part here is understanding how to use a View to generate the options for a generic field. It requires some custom PHP code and some patience to get just right. Let me know if you get stuck somewhere and I can offer some assistance.
Hello, thank you so much for your answer!
I was able to create a generic field with a custom shortcode (using the filter) and to display it in the user form. I'm ready to continue with this challenge. I will indeed need further assistance with this part.
Small question: what kind of user input should I create to store Taxonomy IDs ? Just a simple "Checkboxes" ? If that's the case, I already created it.
If you want to allow a User to select more than one favorite taxonomy term, use checkboxes. If you only allow one favorite taxonomy term, a select or radio field is more appropriate.
Next you will use the CRED API to capture the selected value (or values) and save it into the User's custom field. Here's an example using only one value:
add_action('cred_save_data', 'add_user_favorite_taxonomy_term',10,2);
function add_user_favorite_taxonomy_term($user_id, $form_data) {
// array of form IDs where you want to apply this code
$forms = array( 1234, 5678 );
if ( in_array( $form_data['id'], $forms ) )
{
// capture the selected term value and store it in the user favorite term custom field
$term = $_POST['generic-field-slug'];
update_user_meta($user_id, 'wpcf-custom-field-slug', $term );
}
}
Replace 1234,5678 with a comma-separated list of CRED form IDs where you want to apply this code. Replace 'generic-field-slug' with your generic field slug. Replace 'wpcf-custom-field-slug' with 'wpcf-' plus your user custom field slug. So if your user custom field slug in wp-admin is "fave-term" then this should be 'wpcf-fave-term'. If you allow multiple favorite terms, we can take a look at that code next.
I just did exactly what you told me to do, however, it does not seem to be working properly. When the user submits the registration form, nothing appears in the user profile section..
Okay please check the following:
- When you view the form on the front-end of your site, inspect the generic field markup using the browser inspector. What are the values of each option? They should be either numeric IDs or slugs for each possible term.
- Turn on server logs and add log statements to your cred_save_data hook like so:
add_action('cred_save_data', 'add_user_favorite_taxonomy_term',10,2);
function add_user_favorite_taxonomy_term($user_id, $form_data) {
error_log('add_user_favorite called');
// array of form IDs where you want to apply this code
$forms = array( 1234, 5678 );
if ( in_array( $form_data['id'], $forms ) )
{
error_log('in array');
error_log(print_r($_POST, true));
// capture the selected term value and store it in the user favorite term custom field
$term = $_POST['generic-field-slug'];
error_log('term: ' . $term);
update_user_meta($user_id, 'wpcf-custom-field-slug', $term );
}
}
Replace your slugs and form IDs again if you copy + paste this entire code block. If you are not familiar with server logs, I can show you how to turn them on. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:
define('WP_DEBUG', true);
Then add these lines, just before it says 'stop editing here':
ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
Now submit the CRED form again. This should create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the changes you made to wp-config.php.
Hello there is a problem with this line I guess error_log('term: ' . $term);
Here is the error log:
[08-Feb-2018 23:06:47 UTC] add_user_favorite called
[08-Feb-2018 23:06:47 UTC] in array
[08-Feb-2018 23:06:47 UTC] Array
(
[user_email] => test@test.com
[first_name] => test
[last_name] => test
[categories] => Array
(
[0] => 3
[1] => 26
)
[form_submit_1] => Submit
[_cred_cred_prefix_cred_container_id] => 17
[_cred_cred_prefix_form_id] => 285
[_cred_cred_prefix_form_count] => 1
)
[08-Feb-2018 23:06:47 UTC] PHP Notice: Array to string conversion in /Applications/MAMP/htdocs/loisirs2018/wp-content/themes/salient-child/functions.php on line 210
[08-Feb-2018 23:06:47 UTC] term: Array
Okay let's make a few adjustments.
1. Replace the checkboxes user field with a number field, set to "allow multiple instances of this field". This is where you will store term IDs in the User's profile.
2. Update the PHP code to be:
add_action('cred_save_data', 'add_user_favorite_taxonomy_term',10,2);
function add_user_favorite_taxonomy_term($user_id, $form_data) {
if ($form_data['id'] == 1234)
{
$terms = $_POST['categories'];
foreach($terms as $term) {
add_user_meta($user_id, 'wpcf-custom-field-slug', $term);
}
}
}
Replace 1234 with the ID of your CRED form, and replace "custom-field-slug" with the slug of your repeating number user field. Let me know the results.