Install
This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.
Install straight from GitHub with WP-CLI:
wp plugin install https://github.com/enfrte/wp-cb-block/archive/refs/heads/master.zipA consent checkbox block for WordPress forms. Blocks form submission if unchecked, with conditional visibility based on another field's value.
Files
| File | Purpose |
|---|---|
wp-cb-block.php |
Plugin bootstrap, block registration, server-side rendering, REST & POST validation |
assets/script.js |
Client-side validation, conditional show/hide, error display |
assets/style.css |
Block styling |
languages/ |
Translation files (de_DE, fi_FI, sv_SE) |
Block: wp-cb-block/consent-checkbox
Registered via register_block_type() in wp-cb-block.php:44-67.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
label |
string | "I agree to the terms and conditions" | Checkbox label text |
conditionalField |
string | "" |
name or id of an input field that controls visibility |
Behaviour
- If
conditionalFieldis empty → the checkbox always shows. - If
conditionalFieldis set → the checkbox only appears when a matching input exists in the same form and has a non-empty value. - The block must be placed inside a
<form>element for validation to work. - On form submit, if the checkbox is unchecked → submission is blocked and an error message is shown.
Architecture
Server-side rendering (wp-cb-block.php:69-97)
wp_cb_block_render() runs when the block is output on the frontend:
- Reads
conditionalFieldfrom attributes. - If
conditionalFieldis set, addsdata-conditional-fieldattribute to the wrapper div. - Outputs a checkbox with
name="wp-cb-consent"andrequired. - Includes a hidden
.wp-cb-block-errordiv for the JS error message.
REST API validation (wp-cb-block.php:99-120)
Hooks into rest_pre_dispatch and intercepts POST requests that include wp-cb-consent. If the value is empty, returns a WP_Error with status 400.
Traditional POST validation (wp-cb-block.php:122-139)
Hooks into wp and checks $_POST['wp-cb-consent'] on traditional form submissions. Calls wp_die() with a 400 status if consent is missing.
Client-side validation (assets/script.js)
On DOMContentLoaded:
- Finds all
.wp-block-wp-cb-block-consent-checkboxblocks. - If the block has no
data-conditional-field→ no conditional logic runs; the block stays visible. - If
conditionalFieldis set:- If no matching input exists in the form → the block is hidden.
- Otherwise, listens for
input/changeevents on that field to toggle visibility based on its value.
- On form
submit:- If the block is hidden → skips validation.
- If the checkbox is unchecked → prevents submission, shows
.wp-cb-block-error.
- Hides the error when the user checks the box.
Styling (assets/style.css)
Minimal styles: spacing, inline-flex label layout, red error text. Works with any theme.
How to test
Using the block editor
- Create or edit a page in the block editor.
- Add a Form block (
core/form) or wrap content in a Group block and manually add<form>tags via the Custom HTML block. - Inside the form area, add the Consent Checkbox block.
- Set the Conditional Field attribute to the
nameoridof another input in the same form (e.g.,email). - Add a submit button.
- Publish and visit the page:
- The checkbox is hidden until the conditional field has a value.
- Submitting without checking the box shows the error.
Using the demo page template
A custom page template page-consent-demo.php was created in the theme directory. It wraps page content in a <form> with a submit button, making it possible to test the block even in Full Site Editing themes.
To create a test page:
- Go to Pages → Add New in the admin.
- In the editor, add:
- A Custom HTML block with an input, e.g.:
<label for="email">Email:</label> <input type="email" id="email" name="email" placeholder="Enter your email"> - A Consent Checkbox block (with
conditionalFieldset toemail).
- A Custom HTML block with an input, e.g.:
- In the Page sidebar, under Template, select Consent Checkbox Demo.
- Publish.
Frontend behaviour:
- On load, only the email field is visible. The consent checkbox and submit button are present but the checkbox is hidden.
- Type in the email field → the checkbox appears.
- Clear the field → the checkbox hides again.
- Click Submit without checking the box → the red error "You must agree to continue." appears and submission is blocked.
- Check the box and submit → the form submits.
Using WP-CLI
wp post create \
--post_type=page \
--post_title="Consent Test" \
--post_content='<!-- wp:html --><label for="email">Email:</label> <input type="email" id="email" name="email" placeholder="Enter your email"><!-- /wp:html --><!-- wp:wp-cb-block/consent-checkbox {"conditionalField":"email"} /-->' \
--post_status=publish
Then set the template:
wp post meta update <ID> _wp_page_template page-consent-demo.php
Translations
Available in languages/:
| Locale | File |
|---|---|
| English (source) | wp-cb-block.pot |
| German | wp-cb-block-de_DE.{po,mo} |
| Finnish | wp-cb-block-fi_FI.{po,mo} |
| Swedish | wp-cb-block-sv_SE.{po,mo} |
Strings: "Consent Checkbox", "A checkbox that must be checked to submit the form.", "I agree to the terms and conditions", "You must agree to continue.", "Consent Required".