Gravity forms has one of the most comprehensive, and well documented sets of hooks for developers to use. Almost all hooks that pass a $form
object can be added one of 2 ways, generically which will be called on all forms, or with the ID of a simple form appended, which will then run only on that form.
I’ve never liked having to use the IDs of a Gravity Form in code. It’s a join between something you can see, and something that is tucked away in the backend. If, for instance, a form gets exported, or replaced with a new, imported version, its ID will change, and the join will be broken, resulting in lost functionality.
There is a built in solution to this problem, using the static Gravity Forms function RGFormsModel::get_form_id()
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// we don't want a fatal error if Gravity Forms is disabled if (class_exists('RGFormsModel') { $form_id = RGFormsModel::get_form_id('My Form Name'); if ($form_id) { // form found add_action("gform_pre_render_" . $form_id, "my_custom_handler", 8); add_action("gform_pre_validation_" . $form_id, "my_custom_handler", 10); add_action("gform_admin_pre_render_" . $form_id, "my_custom_handler", 10); add_action("gform_pre_submission_filter_" . $form_id, "my_custom_handler", 10); add_action( "gform_post_data_" . $form_id, "my_custom_handler", 8, 2 ); } } function my_custom_handler($form) { //custom code here } |
This allows you to use names, not IDs in your code. Yes, a form could be renamed, but I’ll take that over an ID, any day.