arraypress / wp-register-fields
Register custom fields on posts, terms, users, quick edit and bulk edit.
Requires
- php: >=8.3
- arraypress/wp-field-kit: ^1.0
Requires (Dev)
- phpcompatibility/phpcompatibility-wp: ^2.1
- phpunit/phpunit: ^12.0
- squizlabs/php_codesniffer: ^3.13.5
- wp-coding-standards/wpcs: ^3.4
README
Add custom fields to any WordPress screen with an array, not a class hierarchy.
What it does
Adding fields to a post, a term, a user profile, quick edit or bulk edit means five different WordPress APIs, each with its own render callback, its own save handler, and its own nonce and capability dance. The field markup is the same every time; only the plumbing differs.
This takes one field definition and handles whichever plumbing applies. Values save themselves, each type sanitises appropriately, and post fields are exposed over the REST API without another line.
Field rendering, types and sanitisation come from wp-field-kit, so a field looks and behaves the same wherever it appears.
Features
- One field definition, usable on posts, terms, users, quick edit and bulk edit
- Over 30 field types, including media pickers, colour, date and WYSIWYG
- Show a field only when another field has a particular value
- Repeatable groups of fields, reorderable by dragging
- Searchable dropdowns for posts, terms and users that load over AJAX
- Hide fields from users who lack a capability, on render and on save
- Bulk edit understands "no change", so an empty field does not wipe a value
- Read values back with helpers that apply the same prefixing rules
Installation
composer require arraypress/wp-register-fields
Quick start
add_action( 'init', function () { register_post_fields( 'product_info', [ 'title' => __( 'Product Information', 'textdomain' ), 'post_types' => 'product', 'fields' => [ 'sku' => [ 'label' => __( 'SKU', 'textdomain' ), 'type' => 'text', ], 'price' => [ 'label' => __( 'Price', 'textdomain' ), 'type' => 'number', 'min' => 0, 'step' => 0.01, ], ], ] ); } ); // Reading it back. $sku = get_post_field_value( $post_id, 'sku' );
The same field array works on the other screens:
register_term_fields( 'category_meta', [ /* ... */ ] ); register_user_fields( 'customer_meta', [ /* ... */ ] ); register_quick_edit_fields( 'product_quick', [ /* ... */ ] ); register_bulk_edit_fields( 'product_bulk', [ /* ... */ ] );
Documentation
Every field type, the conditional-display rules, repeaters, and the full list of configuration options are documented on the docs site.
Requirements
- PHP 8.3 or later
- WordPress 7.1 or later
License
GPL-2.0-or-later