Purpose

Attach custom JavaScript to specific forms. The filter runs inside FormView while it collects scripts for the rendered form, so any ScriptObject you push is enqueued together with the form’s own scripts.

Renamed. This hook used to be called wr_form_add_scripts in earlier versions. The current production name is wr_form_view_add_scriptsadd_filter('wr_form_add_scripts', …) silently no-ops.

Usage

add_filter( 'wr_form_view_add_scripts', array( $this, 'add_script_to_form' ), 10, 2 );

Parameters

  • $scripts (array<ScriptObject>): The scripts already collected for the form.
  • $form (Form): The form being rendered.

Use Case

  • Inject form-specific JavaScript (date pickers, custom validators, third-party widgets).
  • Load a dependency only when a particular form is on the page.

Implementation Example

use TS\WonderfulRelations\System\Form\Models\Form;
use TS\WonderfulRelations\System\Renderer\ScriptObject;
 
add_filter( 'wr_form_view_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( __DIR__ . '/script.js' );
    }
    return $scripts;
}