Stars: 192
Forks: 51
Pull Requests: 53
Issues: 86
Watchers: 135
Last Updated: 2023-08-27 16:21:06
A WordPress plugin that provides an easy way to add custom fields to your object types (post, pages, custom post types, users)
License:
Languages: CSS, PHP, JavaScript, Shell
This code-only developer WordPress plugin allows you to add custom fields to your object types (post, pages, custom post types, users)
This is a WordPress Plugin. We sync changes between github and the WordPress.org plugin repository. Why? Because collaboration is made much easier on github :)
NOTE: The plugin requires WordPress 3.5+
/wp-content/plugins/
directoryBecause the UI thing has been done before. And this more closely aligns with the existing WordPress approach of registering new types of content (post types, taxonomies, etc.)
This is also a developer feature, aimed towards site builders. And real developers don't need UIs ;)
(But really, though, the main benefit of this fact comes into play when you're working with multiple environments, i.e. development/local, qa/staging, production. This approach makes it easy to replicate UIs and features without having to worry about database synchronization and other crazy things.)
For another really well-done, really powerful code-based plugin for managing custom fields, check out Easy Custom Fields and the Custom Metaboxes and Fields For WordPress Class.
add_metadata_field
? Do you really need the stupid x_
?We're being good and "namespacing" our public functions. You should too.
There are usage instructions below
multifield
; see x_add_metadata_multifield()
, props @greatislander, @rinatkhaziev and @PhilippSchreiber for their contributions theretaxonomy_checkbox
and taxonomy_multi_select
field types, props @greatislanderselected()
and checked()
functions in WordPress instead of clumsy if statementsinit
instead of on construct
which runs too earlynumber
, email
, telephone
, datetimepicker
, timepicker
and link
(which uses the WP link manager)upload
field to use the media manager from WordPress 3.5+. Note the upload
field is now readonly
by default (but can be set to false
when you setup the field)required_cap
readonly
paramaterCUSTOM_METADATA_MANAGER_URL
constant$value
for a display_callback
(props @FolioVision)wp_editor()
function (since WP 3.3+) instead of the_editor()
(now deprecated)display_callback
<?
in favor of regular <?php
tags, which caused parse errors on some serversDEFINE
s in to admin_init
so that they can be filtered more easilypassword
, upload
, wysiwyg
, datepicker
, taxonomy_select
, taxonomy_radio
, attachment_list
The main idea behind this plugin is to have a single API to work with regardless of the object type. Currently, Custom Metadata Manager works with user
, comment
and any built-in or custom post types, e.g. post
, page
, etc.
For the sake of performance (and to avoid potential race conditions), always register your custom fields in the custom_metadata_manager_admin_init
hook. This way your front-end doesn't get bogged down with unnecessary processing and you can be sure that your fields will be registered safely. Here's a code sample:
add_action( 'custom_metadata_manager_init_metadata', 'my_theme_init_custom_fields' );
function my_theme_init_custom_fields() {
x_add_metadata_field( 'my_field', array( 'user', 'post' ) );
}
You can get the data as you normally would using the get_metadata
function. Custom Metadata manager stores all data using the WordPress metadata APIs using the slug name you provide. That way, even if you decide to deactivate this wonderful plugin, your data is safe and accessible. For options, you can use get_option
.
Example:
$value = get_metadata( 'post', get_the_ID(), 'featured', true ); // Returns post metadata value for the field 'featured'
A group is essentially a metabox that groups together multiple fields. Register the group before any fields
x_add_metadata_group( $slug, $object_types, $args );
$slug
(string) The key under which the metadata will be stored.$object_types
(string|array) The object types to which this field should be added. Supported: post, page, any custom post type, user, comment.$args = array(
'label' => $group_slug, // Label for the group
'context' => 'normal', // (post only)
'priority' => 'default', // (post only)
'autosave' => false, // (post only) Should the group be saved in autosave? NOT IMPLEMENTED YET!
'exclude' => '', // see below for details
'include' => '', // see below for details
);
x_add_metadata_field( $slug, $object_types, $args );
$slug
(string) The key under which the metadata will be stored. For post_types, prefix the slug with an underscore (e.g. _hidden
) to hide it from the the Custom Fields box.$object_types
(string|array) The object types to which this field should be added. Supported: post, page, any custom post type, user, comment.$args = array(
'group' => '', // The slug of group the field should be added to. This needs to be registered with x_add_metadata_group first.
'field_type' => 'text', // The type of field; 'text', 'textarea', 'password', 'checkbox', 'radio', 'select', 'upload', 'wysiwyg', 'datepicker', 'taxonomy_select', 'taxonomy_radio'
'label' => '', // Label for the field
'description' => '', // Description of the field, displayed below the input
'values' => array(), // Values for select and radio buttons. Associative array
'display_callback' => '', // Callback to custom render the field
'sanitize_callback' => '', // Callback to sanitize data before it's saved
'display_column' => false, // Add the field to the columns when viewing all posts
'display_column_callback' => '', // Callback to render output for the custom column
'required_cap' => '', // The cap required to view and edit the field
'exclude' => '', // see below for details
'include' => '', // see below for details
'multiple' => false, // true or false, can the field be duplicated with a click of a button?
'readonly' => false, // makes the field be readonly (works with text, textarea, password, upload and datepicker fields)
);
You can exclude fields and groups from specific object. For example, with the following, field-1 will show up for all posts except post #123:
$args = array(
'exclude' => 123
);
x_add_metadata_field( 'field-1', 'post', $args );
Alternatively, you can limit ("include") fields and groups to specific objects. The following will ''only'' show group-1 to post #456:
$args = array(
'include' => 123
);
x_add_metadata_group( 'group-1', 'post', $args );
You can pass in an array of IDs:
$args = array(
'include' => array( 123, 456, 789 );
);
With multiple object types, you can pass in an associative array:
$args = array(
'exclude' => array(
'post' => 123,
'user' => array( 123, 456, 789 )
)
);
You can also pass in a callback to programattically include or exclude posts:
$args = array(
'exclude' => function( $thing_slug, $thing, $object_type, $object_id, $object_slug ) {
// exclude from all posts that are in the aside category.
return in_category( 'aside', $object_id );
}
);
$args = array(
'include' => function( $thing_slug, $thing, $object_type, $object_id, $object_slug ) {
// include for posts that are not published.
$post = get_post( $object_id );
return 'publish' != $post->post_status;
}
);
For examples, please see the custom_metadata_examples.php file included with the plugin. Add a constant to your wp-config.php called CUSTOM_METADATA_MANAGER_DEBUG
with a value of true
to see it in action:
define( 'CUSTOM_METADATA_MANAGER_DEBUG', true );
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to:
Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.