Skip Navigation

[Gelöst] Setting a value in CRED form Select field based on User record data

This support ticket is created vor 1 Jahr, 3 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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: Asia/Karachi (GMT+05:00)

Author
Artikel
#2628627

I have a CRED form for entering 'inventory' items. When new Users are created, they are assigned to a particular 'Department'. When a User is adding a new inventory item using the CRED form, I want that User's department to be added to the new item's record and displayed without the User being able to change it. Can this be done using a manually edited Conditional Group on the inventory CRED form?

#2628781

Hi,

Thank you for contacting us and I'd be happy to assist.

To achieve this, you can add one select type field user field 'Users Department' and another select type custom field 'Inventory Department' for the inventory record post type.

Both these fields will have the same options with the same values.

When a new user will be created, a department will be selected for his/her 'Users Department' field.

And in the form to create a new inventory post, you can use the Types fields API shortcode in the 'Inventory Department' field's default value:
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ )


[types usermeta='users-department' output='raw' current_user='true'][/types]

As a result, when the inventory form will load, the department of the current user will automatically be selected. And you can use the custom CSS code to hide this field from the users so that they can't change its value from the form.

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2628803

Thank you for your assistance, Waqar. How would I change this if 'Department' is a taxonomy of the inventory CPT? (This is to simplify the display of inventory items for particular departments with category archives.)

#2629609

is it possible for ‘Department’ in the inventory post to be a taxonomy instead of a select field?

#2629631

Thanks for writing back.

For attaching the department as the taxonomy term, you'll need to add the following changes:

1. In the user field 'Users Department', you'll need to make sure that the individual option values are the same as the 'slugs' of each of the 'Department' taxonomy terms.

2. In the form that adds the 'Inventory' post, you'll add a 'hidden' type generic field with the slug 'department-slug'. In the field's default value, you'll use the same shortcode that gets the value of the 'Department' taxonomy slug from the user field:


[types usermeta='users-department' output='raw' current_user='true'][/types]

3. The last step would be to use a custom function attached to the 'cred_save_data' hook, that gets the slug of the term from the generic field, and attaches it as the taxonomy term of the created 'Inventory' post, when the form submission completes:
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data )


add_action('cred_save_data', 'custom_add_department_term',10,2);
function custom_add_department_term($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12345)
    {
        // target taxonomy slug
        $taxonomy = 'department-tax';

        // term slug value from the generic field
        $term_slug = $_POST['department-slug'];

        // set the term with the created post
        wp_set_post_terms( $post_id, $term_slug, $taxonomy, false );       
    }
}

Note: You'll replace '12345' and 'department-tax', with the actual form ID and the 'Department' taxonomy's slug, used on your website, respectively/

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

#2631981

Sorry for the delay in replying - I have only been able to get back to this topic today. I have been trying the last set of suggestions not having success yet as I'm not sure about the various slugs...

#2632471

I was able to achieve the functionality needed with Waqar's excellent support.