Skip Navigation

[Resolved] Issue using hook for taxonomy

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.

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 2 months ago.

Assigned support staff: Waqar.

Author
Posts
#2382237

I was trying to use the cred_filter_field_attributes_before_add_to_form hook to alter some taxonomy fields. When applying values and returning the attributes, they don't seem to be applied to the field.

The two things I was trying to do was to set a default value for a post form. We have an ability to use settings from the author to pre-populate some taxonomy fields. When setting the 'value' property of the attributes array, that item isn't being selected on the form.

Next, I was trying to work with a hierarchical taxonomy field and was trying to set a specific list of taxonomy items to display. Using the same hook, I was using the 'all' property of the attributes array to reset the taxons I wanted to be displayed and also to change the label of the taxon. I am looping through each taxon in the all property and setting things as I want and then overriding the all with the new array for output, but it isn't taking the changes to apply as I want.

Shouldn't I be able to use this hook to alter the taxonomy for a post form?

#2383551

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

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

The filter "cred_filter_field_attributes_before_add_to_form" is not something that we officially document and recommend for filtering the options of the form fields.

A better alternative with more control would be to replace the taxonomy field in the form, with a generic field, where options are generated using a custom shortcode.

On form submission, you can use the "cred_save_data" hook to attach the selected term(s) from that generic field to the post being added/edited by the form:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

You'll find the example code snippets for a similar approach, in this support thread:
https://toolset.com/forums/topic/adjust-content-of-relationship-field-in-cred-form/

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

regards,
Waqar

#2383821

I would suggest removing filters and action hooks from your code if you are not going to use them. Defeats the purpose of having them as they should be usable in that they are not there just for show.

While I can build out something custom, I was trying to utilize a hook that was in place first before going down a custom route as that tends to increase the time needed to implement something.

Also, this may be the only option to create the custom hierarchical taxonomy field, but what about just setting a default value for a form based on other data? Is there not a way to set this in a hook?

#2384495

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

The filter "cred_filter_field_attributes_before_add_to_form" can be used for the type of filtering that you're trying to apply, but, the downside is that filtering will apply to all the fields. If you'll go this route, you'll end up doing a lot more, to restrict it to a particular taxonomy field, which is why I suggested the alternative approach.

For setting a default/empty value for a taxonomy field, you can use some custom script, and you'll find some examples snippets in these forum threads:
https://toolset.com/forums/topic/cred-blank-empty-default-value-for-taxonomy-select-field/
https://toolset.com/forums/topic/set-the-taxonomy-by-default-when-a-user-fill-the-form/