Skip to content

Commit a4ffe59

Browse files
authored
Expose form post type to REST API (#110)
* Expose form post type to REST API /wp-json/wp/v2/wplf-form * Document usage with REST API
1 parent 221709f commit a4ffe59

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ add your own functionality with [hooks and APIs](#filter--action-api) provided
2929
by Libre Form.
3030

3131
## Try it
32-
[TryoutWP](https://gettryout.com/) has provided us with a live demo, [which you can find here](http://gettryout.com/new/?template=libreform&provider=demo&redirect=wp-admin%2Fpost.php%3Fpost%3D4%26action%3Dedit ). It reflects the current release, not the master branch.
32+
[TryoutWP](https://gettryout.com/) has provided us with a live demo, [which you can find here](http://gettryout.com/new/?template=libreform&provider=demo&redirect=wp-admin%2Fpost.php%3Fpost%3D4%26action%3Dedit ). It reflects the current release, not the master branch.
3333

3434
## Screenshots
3535

@@ -194,6 +194,45 @@ wp_enqueue_script( "themejs", "/path/to/theme.js", array( "wplf-form-js" ), ...
194194
```
195195
Otherwise you might run into errors like "Cannot read property 'push' of undefined".
196196

197+
## REST API driven sites
198+
199+
You can get forms out of the REST API. Just use `/wp/v2/wplf-form` to retrieve forms. You can get a singular form by using filters:
200+
201+
`/wp/v2/wplf-form?slug=form-slug`
202+
203+
However, if you're sending forms from a different domain than WP site URL, you'll run across a CORS issue submitting the form, which you can get around with this:
204+
205+
```php
206+
add_action('wplf_pre_validate_submission', function() {
207+
$origin = $_SERVER['HTTP_ORIGIN'];
208+
header("Access-Control-Allow-Origin: $origin");
209+
header("Access-Control-Allow-Credentials: true");
210+
});
211+
```
212+
Do note that the above code snippet opens your form submissions to the world.
213+
214+
You can also use the "official" JS bundle if you want to.
215+
216+
```javascript
217+
window.ajax_object = {
218+
ajax_url: `${WP.url}/wp-admin/admin-ajax.php`,
219+
ajax_credentials: 'include', // different origin
220+
wplf_assets_dir: `${WP.url}/wp-content/plugins/wp-libre-form/assets`,
221+
}
222+
223+
await new Promise((resolve, reject) => {
224+
const script = document.createElement('script')
225+
const timeout = setTimeout(reject, 30000)
226+
script.src = `${WP.url}/wp-content/plugins/wp-libre-form/assets/scripts/wplf-form.js`
227+
script.onload = (e) => {
228+
clearInterval(timeout)
229+
resolve()
230+
}
231+
232+
document.body.appendChild(script)
233+
})
234+
```
235+
197236
## Multilingual
198237

199238
You can create multilingual forms using Polylang. WPLF will register and automatically fetch the translation when you use special template tags.

classes/class-cpt-wplf-form.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public static function register_cpt() {
101101
'revisions',
102102
'custom-fields',
103103
),
104+
'show_in_rest' => true,
104105
);
105106

106107
register_post_type( 'wplf-form', $args );

0 commit comments

Comments
 (0)