Purpose
Add custom JavaScript scripts to specific forms programmatically. This filter allows you to dynamically attach scripts to forms based on their identifier or other contextual criteria.
Usage
add_action( 'wr_form_add_scripts', array( $this, 'add_script_to_form' ), 10, 2 );
Parameters
$scripts (array)
: An array of script objects that are currently attached to the form.$form (Form)
: The form object, which provides details such as the form identifier.
Use Case
This functionality is useful when:
- You need to include custom JavaScript files or inline scripts for specific forms.
- Enhancing form functionality with external libraries or custom logic is required.
Implementation Example
The following example adds a custom script (script.js
) to a form identified by form_identifier
:
add_action( 'wr_form_add_scripts', array( $this, 'add_script_to_form' ), 10, 2 );
public function add_script_to_form( array $scripts, Form $form ): array {
if ( $form->get_identifier() === "form_identifier" ) {
$scripts[] = ScriptObject::create_by_file( 'path/script.js' );
}
return $scripts;
}