Script localization method for custom modules?

beaver-builder
#1

I love the add_js() method for only enqueueing javascript when my module is on the page. Is there a comparable method like wp_localize_script() that can be used to include extra variables into the javascript when it’s loaded? Thanks!

#2

Welcome!

You should use the enqueue_scripts module method then you can use enqueue and localize normally.

public function enqueue_scripts() {
	wp_enqueue_script( 'test-js', 'http://wwww.example.com/test.js' );
	wp_localize_script( 'test-js', 'test', array( 'test' => 'test' ) );
}