Skip Navigation

[Resolved] How to identify form id in cred_filter_field_before_add_to_form filter

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to use "cred_filter_field_before_add_to_form " hook to add some logic to a selected form and needs to get the form_id.

Solution:
The form id is part of the form_html_id key from the argument passed to the hooked function. Check this example code that extracts the form id from.

function my_test_cred_hook ( $field ) {
    $form_html_id = $field['form_html_id'];
    $form_id = 0;
 
    if ( isset($form_html_id) ) {
        $parts = explode( '_', $form_html_id);
        $form_id = (int) $parts[2];
    }
 
    if ( $form_id > 0 ) {
        error_log( 'form_id ' . $form_id );
    }
}
 
add_filter('cred_filter_field_before_add_to_form', 'my_test_cred_hook', 10, 1);

Relevant Documentation:

This support ticket is created 4 years ago. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

This topic contains 2 replies, has 2 voices.

Last updated by matthewS-9 4 years ago.

Assisted by: Jamal.

Author
Posts
#1590127

Tell us what you are trying to do?

- We use cred_filter_field_before_add_to_form to set a field as required . We have 2 forms with the same field type. We need to access form id to set field requirement as mandatory for one form but let it optional for the other form.

Is there any documentation that you are following?
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_filter_field_before_add_to_form
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

Is there a similar example that we can see?
- https://toolset.com/forums/topic/easiest-way-to-make-a-cred-field-required/

What is the link to your site?
- hidden link

#1590673

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

The form id is part of the form_html_id key from the argument passed to the hooked function. Check this example code that extracts the form id from.

function my_test_cred_hook ( $field ) {
	$form_html_id = $field['form_html_id'];
	$form_id = 0;

	if ( isset($form_html_id) ) {
		$parts = explode( '_', $form_html_id);
		$form_id = (int) $parts[2];
	}

	if ( $form_id > 0 ) {
		error_log( 'form_id ' . $form_id );
	}
}

add_filter('cred_filter_field_before_add_to_form', 'my_test_cred_hook', 10, 1);

I hope this helps. Let me know if you need further assistance.

#1591025

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.