Skip to content

Assets registration

The Assets class is used to register and enqueue assets for your plugin. At the moment, the only asset type supported is javascript files.

The class loads the assets from the build directory and registers them with WordPress.

JavaScript files

To register a javascript file, you can use the register_script() method of the Assets class. This method takes four arguments: file name, whether to enqueue the script, an array of dependencies and if the script should be in footer or not.

NOTE

This class assumes that your scripts are handled by the wp-scripts package. If you are using a different package, you will need to register your scripts manually.

Registering a script

To register a script, you can use the register_script() method of the Assets class. In order to do this, you first need to create a new instance of the Assets class and pass the Plugin instance to it.

php
use Sematico\Metapress\Utils\Assets;
use Sematico\Metapress\Plugin;

$plugin = new Plugin();
$assets = new Assets( $plugin );

Now that you have an instance of the Assets class, you can use the register_script() method to register a script.

php
$assets->register_script( 'my-script', true, [ 'jquery' ], true );

This will register the my-script.js file in the build directory and enqueue it in the footer of the page.

In most cases, you do not need to pass an array of dependencies because the wp-scripts package generates the correct dependencies and creates a sort-of manifest file that is used by the Assets class to register the scripts.

Reference documentation