Skip to content

Commit 7150079

Browse files
authored
Merge pull request #218 from wpengine/docs/create-a-plugin
added docs page for plugin creation
2 parents 65a5458 + 5ab9172 commit 7150079

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export const metadata = {
2+
title: "Create A Plugin",
3+
};
4+
5+
Faust plugins are a structured way to extend or alter the framework's behavior, very much like WordPress plugins do in a traditional WordPress environment. Instead of `PHP`, however, Faust's plugins are written in JavaScript/TypeScript, leveraging actions and filters to deliver similar flexibility in a headless architecture.
6+
7+
## Steps
8+
9+
### 1. Basic setup
10+
11+
If you haven't already, follow the [Basic Setup](/docs/how-to/basic-setup/) steps to get Faust.js set up.
12+
13+
### 2. Create A Plugins Folder and file
14+
15+
In its very basic form, a Faust Plugin is a JavaScript class with an `apply` method. This apply method has a parameter called `hooks`, which is passed from `@wordpress/hooks`. Create a folder in the root of your Faust project called `plugin`. In the folder, create a file called `SamplePlugin.js`. In this file, add this code block:
16+
17+
```js title="plugins/SamplePlugin.js"
18+
import { FaustHooks } from "@faustwp/core";
19+
20+
export class MyPlugin {
21+
/**
22+
* @param {FaustHooks} hooks
23+
*/
24+
apply(hooks) {
25+
// Plugin logic goes here
26+
}
27+
}
28+
```
29+
30+
The `hooks` parameter is an object which contains the functions `addFilter` and `addAction`, amongst others. You can then use these functions to call the respective actions and filters to tap into Faust like so:
31+
32+
```js {10-16} title="plugins/SamplePlugin.js "
33+
import { FaustHooks } from "@faustwp/core";
34+
35+
export class MyPlugin {
36+
/**
37+
* @param {FaustHooks} hooks
38+
*/
39+
apply(hooks) {
40+
const { addAction, addFilter } = hooks;
41+
42+
addFilter(
43+
"possibleTemplatesList",
44+
"my-namespace",
45+
(possibleTemplates, context) => {
46+
// Filter logic goes here.
47+
},
48+
);
49+
}
50+
}
51+
```
52+
53+
> [!important] Please Note
54+
> `experimentalPlugins` is being deprecated and replaced with `plugins` in the `faust.config.js` file.
55+
> Please update your configuration accordingly.
56+
57+
### 3. Add The Plugin to the Faust Config
58+
59+
You can then implement the plugin by adding it to the plugins property in the App's `faust.config.js` file:
60+
61+
```js title="faust.config.js"
62+
import { setConfig } from "@faustwp/core";
63+
import templates from "./wp-templates";
64+
import possibleTypes from "./possibleTypes.json";
65+
import { MyPlugin } from "./plugins/MyPlugin.js";
66+
67+
/**
68+
* @type {import('@faustwp/core').FaustConfig}
69+
*/
70+
export default setConfig({
71+
templates,
72+
plugins: [new MyPlugin()],
73+
possibleTypes,
74+
});
75+
```

src/pages/docs/nav.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
{
2828
"title": "Post Previews",
2929
"route": "/docs/how-to/post-previews/"
30+
},
31+
{
32+
"title": "Create A Plugin",
33+
"route": "/docs/how-to/create-a-plugin/"
3034
}
3135
]
3236
},

0 commit comments

Comments
 (0)