Jfb form sidebar panel

From Lonsdale201/wp-agent-skills · View interactive version

Jfb form sidebar panel is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Adds a per-form settings panel to the JetFormBuilder Gutenberg form editor sidebar — registers REST-exposed post meta on the form CPT, enqueues a block-editor JS bundle, and registers a panel via the JFB-specific 'jet.fb.register.plugins' filter using @wordpress/components (TextControl, SelectContro

How to use this skill

Skill file

JetFormBuilder: form-level settings panel in the Gutenberg sidebar

JetFormBuilder forms are stored as a custom post type (jet-form-builder) edited in the standard Gutenberg block editor. Companion plugins add per-form settings by injecting a panel into the editor's document sidebar — the panel reads and writes post meta on the form, so each form gets its own configuration independently of the plugin's global Settings page.

This skill covers that subsystem. It is distinct from the global plugin Settings page covered in the jfb-settings-tab skill — that one uses Vue + cx-vui components, this one uses WordPress packages (@wordpress/components, React under the hood) and standard post-meta REST exposure.

A typical JFB companion plugin (e.g. media-storage-for-jetformbuilder) ships both: a global tab for site-wide defaults, and a per-form sidebar panel for overrides. See the Dual-mode pattern section below.

API stability note

The register_post_meta + REST exposure pattern is standard WordPress and stable since WP 4.9. The JFB-specific pieces — the 'jet-form-builder/editor-assets/before' PHP action, the 'jet.fb.register.plugins' JS filter, and the useMetaState hook exposed on window.JetFBHooks — are stable across the JFB 3.x line in the source observed. The plugin-version-tested field records last end-to-end verification only.

When to use this skill

Architecture in one paragraph

The JFB form CPT (jet-form-builder) registers post meta keys with 'show_in_rest' => true and an auth_callback, making them readable and writable through the WordPress REST API. The Gutenberg post editor automatically syncs registered REST meta with its data store, so any UI bound to that meta auto-saves when the user clicks "Update". JFB exposes a JS filter 'jet.fb.register.plugins' that collects panel definitions, each containing a render function. Inside the render function, the panel calls JFB's useMetaState hook (a thin wrapper around Gutenberg's useEntityProp) to bind component state to a meta key, then renders WP standard form controls.

Step 1 — PHP: register the post meta

<?php
namespace MyPlugin\FormSettings;

class FormMeta {

    const FORM_CPT = 'jet-form-builder';
    const META_KEY = '_myplugin_form_settings';

    public function register() {
        add_action( 'init', array( $this, 'register_meta' ) );
    }

    public function register_meta() {
        register_post_meta(
            self::FORM_CPT,
            self::META_KEY,
            array(
                'type'              => 'string',
                'single'            => true,
                'default'           => wp_json_encode( $this->defaults() ),
                'show_in_rest'      => array(
                    'schema' => array( 'type' => 'string' ),
                ),
                'auth_callback'     => static function ( $allowed, $meta_key, $post_id, $user_id ) {
                    return user_can( $user_id, 'edit_post', $post_id );
                },
                'sanitize_callback' => array( $this, 'sanitize' ),
            )
        );
    }

    public function defaults() {
        return array(
            'enabled'         => false,
            'target_folder'   => '',
            'max_filesize_mb' => null,
        );
    }

    public function sanitize( $raw ) {
        $decoded = json_decode( (string) $raw, true );
        if ( ! is_array( $decoded ) ) {
            return wp_json_encode( $this->defaults() );
        }
        // Coerce types per known schema.
        $clean = array(
            'enabled'         => ! empty( $decoded['enabled'] ),
            'target_folder'   => sanitize_text_field( $decoded['target_folder'] ?? '' ),
            'max_filesize_mb' => isset( $decoded['max_filesize_mb'] )
                ? (float) $decoded['max_filesize_mb']
                : null,
        );
        return wp_json_encode( $clean );
    }
}

Critical points:

Step 2 — PHP: enqueue the editor assets

JFB fires 'jet-form-builder/editor-assets/before' when the form editor loads its scripts. Hook into this rather than enqueue_block_editor_assets directly — it ensures your bundle loads in the right order, after JFB's own data module is registered.

add_action(
    'jet-form-builder/editor-assets/before',
    function () {
        $handle = 'myplugin-form-editor';

        wp_enqueue_script(
            $handle,
            plugins_url( 'assets/js/form-editor.js', __FILE__ ),
            array(
                'wp-hooks',
                'wp-element',
                'wp-components',
                'wp-data',
                'wp-i18n',
                'jet-fb-data', // exposes window.JetFBHooks (incl. useMetaState)
            ),
            '1.0.0',
            true
        );

        if ( function_exists( 'wp_set_script_translations' ) ) {
            wp_set_script_translations( $handle, 'myplugin' );
        }

        // Pass site-wide defaults and labels to the panel.
        wp_localize_script(
            $handle,
            'MyPluginFormPanel',
            array(
                'metaKey'        => '_myplugin_form_settings',
                'globalDefaults' => array(
                    'max_filesize_mb' => (float) get_option( 'myplugin_max_filesize_mb', 0 ),
                ),
                'labels'         => array(
                    'panelTitle' => __( 'My Plugin', 'myplugin' ),
                    'enabled'    => __( 'Enable for this form', 'myplugin' ),
                    'folder'     => __( 'Target folder', 'myplugin' ),
                    'filesize'   => __( 'Max file size (MB)', 'myplugin' ),
                ),
            )
        );
    }
);

The jet-fb-data script handle is what exposes window.JetFBHooks.useMetaState. Declare it as a dependency even though JFB usually loads it first — the dependency is the contract.

Step 3 — JS: register the sidebar panel

import { addFilter } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
import { Fragment, createElement as el } from '@wordpress/element';
import { ToggleControl, TextControl, RangeControl, Notice } from '@wordpress/components';

const { metaKey, globalDefaults, labels } = window.MyPluginFormPanel || {};
const useMetaState = window.JetFBHooks && window.JetFBHooks.useMetaState;

if ( ! useMetaState ) {
    // JFB data module not loaded — fail silently rather than crashing the editor.
    return;
}

function MyPluginPanel() {
    const [ raw, setRaw ] = useMetaState( metaKey, '{}', [] );

    const state = ( () => {
        try { return JSON.parse( raw ); } catch ( e ) { return {}; }
    } )();

    const update = ( patch ) => {
        setRaw( JSON.stringify( { ...state, ...patch } ) );
    };

    return el( Fragment, null,
        el( ToggleControl, {
            label:    labels.enabled,
            checked:  !! state.enabled,
            onChange: ( v ) => update( { enabled: v } ),
        } ),
        el( TextControl, {
            label:    labels.folder,
            value:    state.target_folder || '',
            onChange: ( v ) => update( { target_folder: v } ),
            disabled: ! state.enabled,
        } ),
        el( RangeControl, {
            label:    labels.filesize,
            value:    state.max_filesize_mb ?? globalDefaults.max_filesize_mb,
            min:      0,
            max:      500,
            step:     0.5,
            onChange: ( v ) => update( { max_filesize_mb: v } ),
            help:     state.max_filesize_mb == null && globalDefaults.max_filesize_mb > 0
                ? __( 'Using global default. Change here to override.', 'myplugin' )
                : undefined,
        } )
    );
}

addFilter(
    'jet.fb.register.plugins',
    'myplugin/form-panel',
    ( plugins = [] ) => {
        const def = {
            base: {
                name:  'myplugin-form-panel',
                title: labels.panelTitle,
            },
            settings: {
                icon:   'admin-generic',
                render: MyPluginPanel,
            },
        };

        // Idempotency guard — addFilter callbacks can run more than once.
        if ( ! plugins.some( ( p ) => p.base.name === def.base.name ) ) {
            plugins.push( def );
        }
        return plugins;
    }
);

`useMetaState` — what it actually does

useMetaState( metaKey, defaultValue, deps ) is a JFB-specific React hook exposed at window.JetFBHooks.useMetaState. It wraps useEntityProp from @wordpress/core-data to bind a component to a single post-meta key on the currently-edited post. It returns [ value, setValue ] like useState, but writes go through the WP data store and trigger Gutenberg's "post is dirty" indicator, so saving is automatic on the next "Update" click.

Treat the value as an opaque string in the hook layer; parse JSON in your component, write JSON back via setValue( JSON.stringify( ... ) ). Wrapping JSON.parse in try/catch is mandatory — corrupt or partially-saved meta should not crash the editor.

If window.JetFBHooks.useMetaState is undefined (JFB data module failed to load, version mismatch, etc.), the panel must fail silently. Don't throw, don't render an error UI inside the editor — that just confuses the user.

Native components — use these 1:1

These are from @wordpress/components and work without modification in the JFB sidebar context. They are not the same as the cx-vui-* set used in the global Settings page.

| Component | Purpose | Common props |

|---|---|---|

| TextControl | Single-line text input | label, value, onChange, help, placeholder, disabled |

| TextareaControl | Multi-line text | label, value, onChange, rows, help |

| ToggleControl | Boolean on/off | label, checked, onChange, help |

| CheckboxControl | Boolean checkbox | label, checked, onChange |

| SelectControl | Dropdown | label, value, options: [{ label, value }], onChange |

| RadioControl | Radio group | label, selected, options, onChange |

| RangeControl | Numeric slider | label, value, min, max, step, onChange |

| Notice | Inline alert / info | status: 'info' \| 'warning' \| 'error' \| 'success', isDismissible |

| PanelBody | Collapsible section inside a panel | title, initialOpen |

| Button | Action button | variant: 'primary' \| 'secondary' \| 'tertiary', onClick |

| Spinner | Loading indicator | (no props) |

The full component catalog is on the WordPress block editor handbook page; the list above is the subset confirmed in the wild for JFB sidebar panels.

Beyond the native set — "hack zone"

@wordpress/components covers most form UI needs. If you need something not in the table:

Preferred: compose existing components

Most "complex" UI (a key/value pair editor, a list of items, a conditional reveal) is just a loop of TextControl + Button inside a PanelBody. Compose first.

Acceptable: bundle your own React component

For genuinely custom UI (a code editor, a JSON tree view, an OAuth-aware connector card) use @wordpress/element (which is React under another name) and write your own component. Keep it inside your plugin's bundle.

Avoid: Vue or cx-vui inside the sidebar

The JFB sidebar is React. Don't try to mount Vue components or pull in cx-vui-* here — that's the global Settings page world. Mixing the two breaks reactivity and confuses users.

Avoid: directly mutating Gutenberg state

Always go through useMetaState (or useEntityProp if you have a reason). Calling wp.data.dispatch( 'core/editor' ).editPost(...) directly works but bypasses the meta-key abstraction and makes save flow harder to reason about.

Storage format — one JSON blob per feature

Per the JFB convention, store all your form-level config under one meta key as a JSON-encoded string. Concrete row in wp_postmeta:

post_id  | meta_key                   | meta_value
123      | _myplugin_form_settings    | {"enabled":true,"target_folder":"uploads","max_filesize_mb":50}

Don't split into _myplugin_enabled, _myplugin_folder, _myplugin_filesize etc. — that bloats wp_postmeta, multiplies REST schema entries, and complicates atomic saves.

Dual-mode pattern: global default + per-form override

A common requirement: "if the form sets a value, use it; otherwise fall back to the plugin's site-wide setting." JFB does not provide a built-in merge utility — you implement it in your plugin's runtime code.

The convention observed in production companion plugins:

  1. Global defaults live in the plugin Settings page (jfb-settings-tab skill), stored in wp_options.
  2. Per-form overrides live in post meta, stored as JSON under a single key.
  3. Both are passed to the panel JS via wp_localize_script — the global as globalDefaults, the meta key name as metaKey.
  4. In the panel, when a per-form value is null / unset, render placeholder or help text showing the global default ("Using global default. Change here to override.").
  5. On the frontend (form rendering / submission handler), implement the merge:
function get_effective_setting( $form_id, $key ) {
    $form_raw  = get_post_meta( $form_id, '_myplugin_form_settings', true );
    $form_data = $form_raw ? json_decode( $form_raw, true ) : array();
    if ( isset( $form_data[ $key ] ) && $form_data[ $key ] !== null ) {
        return $form_data[ $key ];
    }
    return get_option( 'myplugin_' . $key );
}

Keep the merge logic in one function called from every consumer. Otherwise inevitably one consumer reads only the form meta, another reads only the global, and they drift.

Critical rules

Common pitfalls (failure modes inferred from the API contract)

Cross-references

What this skill does NOT cover

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.