• Stars
    star
    191
  • Rank 195,657 (Top 4 %)
  • Language
    PHP
  • Created over 12 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A WordPress plugin that provides an easy way to add custom fields to your object types (post, pages, custom post types, users)

Custom Metadata Manager for WordPress

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+

Installation

  1. Install through the WordPress admin or upload the plugin folder to your /wp-content/plugins/ directory
  2. Activate the plugin through the 'Plugins' menu in WordPress
  3. Add the necessary code to register your custom groups and fields to your functions.php or plugin.
  4. Enjoy!

Frequently Asked Questions

Why a code-based approach instead of a UI?

Because 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.

Why isn't the function just add_metadata_field? Do you really need the stupid x_?

We're being good and "namespacing" our public functions. You should too.

How do I use this plugin?

There are usage instructions below

Changelog

0.8 (currently under development)

  • added ability to group several fields as a multifield; see x_add_metadata_multifield(), props @greatislander, @rinatkhaziev and @PhilippSchreiber for their contributions there
  • allow field types that save as multiples but don't display as cloneable or multiples
  • added the taxonomy_checkbox and taxonomy_multi_select field types, props @greatislander
  • made use of the selected() and checked() functions in WordPress instead of clumsy if statements
  • limit or exclude groups and fields using a custom callback
  • adjusted the copyright to include 2013 and to list "The Contributors" instead of specific individuals
  • adjusted the list of contributors in the plugin
  • adjusted the plugin URL and removed the donate URL
  • adjusted files for code standards
  • fixed PHP warning with empty values for date fields
  • moved filtering of instance vars to init instead of on construct which runs too early
  • added new field types: number, email, telephone, datetimepicker, timepicker and link (which uses the WP link manager)
  • added ability to add default value for certain field types
  • added ability to set placeholder for certain fields
  • updated the examples file
  • rewrote the 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)
  • updated JavaScript to be up to standard with coding standards and be fully compatible with jQuery 1.9+
  • replaced chosen.js with select2.js
  • reformat and clean up css file
  • added ability for groups to display a description
  • added ability to limit capabilities for entire groups using required_cap
  • convert plugin class to singleton

0.7

  • added the ability to have readonly fields with the new readonly paramater

0.6

  • note: the plugin now requires WordPress 3.3+ (chiefly for the wysiwyg & datepicker fields)
  • update/clean-up the examples file
  • properly enqueue admin css for WP 3.3+
  • added a filter for the CUSTOM_METADATA_MANAGER_URL constant
  • fix fields not appearing when editing users in WP 3.3+ (props @FolioVision)
  • now passing the $value for a display_callback (props @FolioVision)
  • use the new wp_editor() function (since WP 3.3+) instead of the_editor() (now deprecated)
  • wysiwyg fields are no longer cloneable (may be revisited in a future version)
  • note: metaboxes that have a wysiwyg field will break when moved, this is not a bug per-se (may be revisited in a future version)
  • password fields are now cloneable
  • added filters for most of the plugin's internal variables
  • now using WordPress' built-in jQuery UI for the datepicker field
  • updated the screenshots
  • updated the instructions in readme.txt

0.5.7

  • pass additional params for display_callback

0.5.6

  • fix bugs with datepicker

0.5.5

  • remove all whitespace
  • fix some bugs with the tinymce field

0.5.4

  • fix display_callback for fields

0.5.3

  • removed php opening shorttags <? in favor of regular <?php tags, which caused parse errors on some servers

0.5.2

  • better tiny mce implementation and added html/visual switch
  • small css fixes and added inline documentation
  • moved DEFINEs in to admin_init so that they can be filtered more easily

0.5.1

  • Bug fix with group context on add meta box
  • Remove few lines of old code left-over from 0.4

0.5

  • Making the changes from 0.4 public
  • Removed ability to generate option pages; after further consideration this is out of scope for this project
  • Removed attachment_list field, useless
  • Dates now save as unix timestamp
  • Taxonomy fields now save as both a custom field and as their proper taxonomy (will consider adding the ability to enable/disable this in a future version)
  • Multiplied fields no longer save as a serialized array, instead they save as multiple metadata with the same key (metadata api supports multiples!) - remember to set the last param to false to get multiple values.
  • NOTE: currently multiplied fields will display out of order after saving, however this should not affect anything else other than the admin, should be fixed soon
  • Other small improvements

0.4

  • Enhanced the code which generates the different field types
  • Added new types: password, upload, wysiwyg, datepicker, taxonomy_select, taxonomy_radio, attachment_list
  • Added field multiplication ability
  • Metadata is now deleted if a value is empty
  • Can now also generate option pages which use a metabox interface

0.3

  • Can now limit or exclude fields or groups from specific ids
  • Added updated screenshots and new code samples!
  • Bug fix: the custom display examples weren't working well
  • Bug fix: fields not showing on "Add New" page. Thanks Jan Fabry!
  • Bug fix: fields not showing on "My Profile" page. Thanks Mike Tew!

0.2

  • Added a textarea field type
  • Added support for comments (you can now specify comments as an object type)
  • Added basic styling for fields so that they look nice

0.1

  • Initial release

Usage

Object Types

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.

Registering your fields

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' ) );
}

Getting the data

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'

Adding Metadata Groups

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 );

Parameters

  • $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.

Options and Overrides

$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
);

Adding Metadata Fields

x_add_metadata_field( $slug, $object_types, $args );

Parameters

  • $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.

Options and Overrides

$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)
);

Include / Exclude

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;
	}
);

Examples

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 );

License

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.

More Repositories

1

mongoose

MongoDB object modeling designed to work in an asynchronous environment.
JavaScript
26,622
star
2

wp-calypso

The JavaScript and API powered WordPress.com
JavaScript
12,359
star
3

_s

Hi. I'm a starter theme called _s, or underscores, if you like. I'm a theme meant for hacking so don't use me as a Parent Theme. Instead try turning me into the next, most awesome, WordPress theme out there. That's what I'm here for.
CSS
10,849
star
4

node-canvas

Node canvas is a Cairo backed Canvas implementation for NodeJS.
JavaScript
9,820
star
5

kue

Kue is a priority job queue backed by redis, built for node.js.
JavaScript
9,434
star
6

simplenote-electron

Simplenote for Web, Windows, and Linux
TypeScript
4,517
star
7

juice

Juice inlines CSS stylesheets into your HTML source.
JavaScript
3,037
star
8

pocket-casts-android

Pocket Casts Android ๐ŸŽง
Kotlin
2,440
star
9

cli-table

Pretty unicode tables for the CLI with Node.JS
JavaScript
2,243
star
10

expect.js

Minimalistic BDD-style assertions for Node.JS and the browser.
JavaScript
2,098
star
11

simplenote-ios

Simplenote for iOS
Swift
1,976
star
12

monk

The wise MongoDB API
JavaScript
1,845
star
13

knox

S3 Lib
JavaScript
1,738
star
14

simplenote-android

Simplenote for Android
Java
1,688
star
15

jetpack

Security, performance, marketing, and design tools โ€” Jetpack is made by WordPress experts to make WP sites safer and faster, and help you grow your traffic.
PHP
1,550
star
16

pocket-casts-ios

Pocket Casts iOS app ๐ŸŽง
Swift
1,464
star
17

simplenote-macos

Simplenote for macOS
Swift
1,420
star
18

antiscroll

OS X Lion style cross-browser native scrolling on the web that gets out of the way.
JavaScript
1,079
star
19

wp-desktop

WordPress.com for Desktop
981
star
20

WP-Job-Manager

Manage job listings from the WordPress admin panel, and allow users to post jobs directly to your site.
PHP
874
star
21

browser-repl

Launch a repl on your command line to any browser in the cloud.
JavaScript
728
star
22

themes

Free WordPress themes made by Automattic for WordPress.org and WordPress.com.
CSS
693
star
23

legalmattic

Democratizing WordPress.com legalese since 2014!
672
star
24

wpcom.js

WordPress.com JavaScript API client designed for Node.js and browsers
JavaScript
658
star
25

Picard

A prototype theme that uses React and WP-API
CSS
631
star
26

fb-instant-articles

Archived (see Readme). Enable Facebook Instant Articles on your WordPress site.
PHP
628
star
27

sensei

Sensei LMS - Online Courses, Quizzes, & Learning
PHP
514
star
28

developer

In your WordPress, developing locally
PHP
470
star
29

wordpress-activitypub

ActivityPub for WordPress
PHP
442
star
30

theme-components

A collection of patterns for creating a custom starter WordPress theme.
PHP
404
star
31

wp-super-cache

WP Super Cache: A fast caching engine for WordPress
PHP
399
star
32

Edit-Flow

WordPress plugin to accelerate your editorial workflow
PHP
341
star
33

o2

The o2 plugin for WordPress โ€” blogging at the speed of thought
JavaScript
332
star
34

newspack-plugin

An advanced open-source publishing and revenue-generating platform for news organizations.
PHP
310
star
35

liveblog

Liveblogging done right. Using WordPress.
PHP
304
star
36

batcache

A memcached HTML page cache for WordPress.
PHP
278
star
37

Co-Authors-Plus

Multiple bylines and Guest Authors for WordPress
PHP
275
star
38

newspack-theme

A theme for Newspack.
PHP
265
star
39

vip-quickstart

Retired
PHP
265
star
40

Iris

A(n awesome) Color Picker
JavaScript
257
star
41

babble

Multilingual WordPress done right.
PHP
244
star
42

syntaxhighlighter

WordPress plugin that makes it easy to post syntax-highlighted code snippets.
CSS
237
star
43

VIP-Coding-Standards

PHP_CodeSniffer ruleset to enforce WordPress VIP coding standards.
PHP
210
star
44

underscores.me

PHP
209
star
45

newspack-blocks

Gutenberg blocks for the Newspack project.
PHP
193
star
46

isolated-block-editor

Repackages Gutenberg's editor playground as a full-featured multi-instance editor that does not require WordPress.
CSS
192
star
47

camptix

Moved to https://github.com/WordPress/wordcamp.org/
PHP
182
star
48

browserbuild

JavaScript
170
star
49

woocommerce-payments

Accept payments via credit card. Manage transactions within WordPress.
PHP
162
star
50

vip-go-mu-plugins

The development repo for mu-plugins used on the WordPress VIP Platform.
PHP
158
star
51

Documattic

WordPress presentations and resources shared by WordPress.com VIP
JavaScript
156
star
52

google-docs-add-on

Publish to WordPress from Google Docs
JavaScript
152
star
53

mydb

JavaScript
150
star
54

vip-scanner

Deprecated: Scan all sorts of themes and files and things! Use PHPCS and the VIP coding standards instead
PHP
140
star
55

wp-memcached

Memcached Object Cache for WordPress.
PHP
139
star
56

Genericons

A public mirror of changes to the Genericon release.
CSS
136
star
57

regenerate-thumbnails

WordPress plugin for regenerating thumbnails of uploaded images. Over 1 million active users and counting.
PHP
131
star
58

media-explorer

With Media Explorer, you can now search for tweets and videos on Twitter and YouTube directly from the Add Media screen in WordPress.
PHP
124
star
59

Rewrite-Rules-Inspector

WordPress plugin to inspect your rewrite rules.
PHP
123
star
60

PhpStorm-Resources

PhpStorm is making inroads at Automattic. Here you'll find various helpful files we've made.
123
star
61

wordbless

WorDBless allows you to use WordPress core functions in your PHPUnit tests without having to set up a database and the whole WordPress environment
PHP
122
star
62

vip-go-mu-plugins-built

The generated repo for mu-plugins used on the VIP Go platform.
PHP
120
star
63

social-logos

A repository of all the social logos we use on WordPress.com
JavaScript
119
star
64

Cron-Control

A fresh take on running WordPress's cron system, allowing parallel processing
PHP
116
star
65

block-experiments

A monorepo of Block Experiments
JavaScript
114
star
66

genericons-neue

Genericons Neue are generic looking icons, suitable for a blog or simple website
HTML
114
star
67

nginx-http-concat

WordPress plugin to perform CSS and JavaScript concatenation of individual script files into one resource request.
PHP
114
star
68

php-thrift-sql

A PHP library for connecting to Hive or Impala over Thrift
PHP
113
star
69

wp-e2e-tests

Automated end-to-end tests for WordPress.com
JavaScript
112
star
70

ad-code-manager

Easily manage the ad codes that need to appear in your templates
PHP
112
star
71

gridicons

The WordPress.com icon set
PHP
108
star
72

woocommerce-services

WooCommerce Services is a feature plugin that integrates hosted services into WooCommerce (3.0+), and currently includes automated tax rates and the ability to purchase and print USPS shipping labels.
JavaScript
104
star
73

es-backbone

ElasticSearch Backbone library for quickly building Faceted Search front ends.
JavaScript
103
star
74

syndication

Syndicate your WordPress content.
PHP
102
star
75

theme-tools

Tools for making better themes, better.
JavaScript
99
star
76

mShots

Website Thumbnail/Snapshot Service
JavaScript
94
star
77

prefork

PHP class for pre-loading heavy PHP apps before serving requests
PHP
94
star
78

phpcs-neutron-standard

A set of phpcs sniffs for PHP >7 development
PHP
93
star
79

newspack-newsletters

Author email newsletters in WordPress
PHP
89
star
80

musictheme

A theme for bands and musicians that uses an experimental Gutenberg layout.
CSS
89
star
81

gutenberg-themes-sketch

A set of Sketch files to help you design block-driven WordPress themes.
88
star
82

zoninator

Curation made easy! Create "zones" then add and order your content straight from the WordPress Dashboard.
PHP
85
star
83

cloudup-cli

cloudup command-line executable
JavaScript
83
star
84

go-search-replace

๐Ÿš€ Search & replace URLs in WordPress SQL files.
Go
81
star
85

lazy-load

Lazy load images on your WordPress site to improve page load times and server bandwidth.
JavaScript
77
star
86

gutenberg-ramp

Control conditions under which Gutenberg loads - either from your theme code or from a UI
PHP
75
star
87

measure-builds-gradle-plugin

Gradle Plugin for reporting build time metrics.
Kotlin
74
star
88

auto-update

Objective-C
73
star
89

wpes-lib

WordPress-Elasticsearch Lib
PHP
73
star
90

vip-go-skeleton

The base repository structure for all VIP Go sites
PHP
72
star
91

msm-sitemap

Comprehensive sitemaps for your WordPress VIP site. Joint collaboration between Metro.co.uk, WordPress VIP, Alley Interactive, Maker Media, 10up, and others.
PHP
70
star
92

jurassic.ninja

A frontend to launching ephemeral WordPress instances that auto-destroy after some time
PHP
69
star
93

gutenberg-block-styles

An example of a simple plugin that adds a block style to Gutenberg.
PHP
68
star
94

atd-chrome

After the Deadline extension for Chrome
JavaScript
66
star
95

wp-api-console

WordPress (.com and .org) API Console written in React/Redux
JavaScript
66
star
96

eventbrite-api

The Eventbrite API plugin brings the power of Eventbrite to WordPress, for both users and developers.
PHP
65
star
97

mongo-query

mongo query API component
JavaScript
65
star
98

site-logo

Add a logo to your WordPress site. Set it once, and all themes that support it will display it automatically.
PHP
65
star
99

vip-go-nextjs-skeleton

A Next.js boilerplate for decoupled WordPress on VIP.
TypeScript
65
star
100

newspack-popups

AMP-compatible popup notifications.
PHP
61
star