For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

Create package

As mentioned here and here, we are using a multi-package-/mono repository that means we can use the advantages of modularized code. What does this exactly mean? Code, which you use more than once, put it into an own package. This works for TypeScript code and also for PHP code.

Beside the packages/utils package you can create another package. It is recommend to not modify the utils package so you can easier migrate to new versions of WP ReactJS Starter. So, let's start and create a new package:

create-wp-react-app create-package

You will get some prompts and afterwards a new package will be available in packages/.

Consume package PHP in plugin

The previously created package contains a src folder. There you can put all your shared PHP classes. Each created plugin can consume local packages from packages/ as the path is configured already properly.

cd plugins/my-plugin # Navigate to our plugin
composer require "wp-reactjs-multi-starter/mypackage @dev" # @dev is important so symlinking works as expected!

Do not forget navigating to your plugin which consumes the dependency and add the dependency path to .wprjss only changes.

Example

So, let's imagine we have the file packages/mypackage/src/Example.php. In your plugins' coding you can access the class simply with the defined PHP namespace:

new \MatthiasWeb\MyPackage\Example();

You have to replace wp-reactjs-multi-starter / MatthiasWeb\MyPackage with your names.

Dynamically get plugin data

What? You heard correctly, you can dynamically get plugin data in your composer package. Due to the fact you inject UtilsProvider in all your classes you can get dynamically plugin data. That means, if you call a method of a modularized composer package in your plugin, you simply can obtain a constant like this in your composer package:

Consume package JavaScript in plugin

The previously created package also contains a lib folder. There you can put all your shared TypeScript files. Due to the fact we are using lerna using packages as simple as:

Afterwards you need to enqueue that package to your plugin. Why? All packages are so-called externals and WordPress handles it with an own system: Each package / module has an own handle and exposes functionality. Navigate to your Assets.php and enqueue the package:

In your plugins' coding you can access the files simply as follow:

Do not forget navigating to your plugin which consumes the dependency and add the dependency path to .wprjss only changes.

You have to replace wp-reactjs-multi-starter with your names.

Localization

A package can also be localized with commands yarn i18n:backend and yarn i18n:generate:frontend. All .pot files will be generated to languages. If you consume a package via enqueueComposerScript and composer require all localization files are automatically loaded to the WordPress runtime, you do not have to manually load them!

Last updated