[WordPress] 外掛分享: Custom Metadata Manager

WordPress 外掛 Custom Metadata Manager 的封面圖片。

前言介紹

  • 這款 WordPress 外掛「Custom Metadata Manager」是 2010-10-17 上架。
  • 目前有 700 個安裝啟用數。
  • 上一次更新是 2012-07-11,距離現在已有 4679 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 3.3 以上版本才可以安裝。
  • 有 5 人給過評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

batmoo | cvernon | jkudish | stresslimit |

外掛標籤

postmeta | post meta | user meta | custom metadata | custom metadata manager metadata |

內容簡介

這是一個方便的 WordPress 外掛程式,可讓你輕鬆地為你的對象類型(文章、頁面、自訂文章類型、使用者)添加自訂欄位,並且產生選項頁面。

這個外掛的目的是要協助你快速地建立為使用者熟悉直觀的介面,以符合 WordPress 的風格。

自訂欄位面板雖然不錯,但對使用者來說可能不太容易使用。如果你要添加自己的 metaboxes 和欄位,可能需要花費大量的時間和重複的程式碼,而這些時間和工作可以用在其他地方。

這個外掛可以在幕後處理所有這些沉重的工作,讓你更專注於建立和連接你的資料而不用擔心這些細節。這段程式碼 x_add_metadata_field('my-field-name', 'post') 就會產生一個 metabox,其中包含一個文字欄位,並具有儲存輸入值所需的鉤子。

API 的用法類似於註冊自訂文章類型和分類法的用法,因此應該是熟悉的領域。

注意:這個外掛現在需要 WordPress 3.3+(主要是為了 WYSIWYG 和日期選擇欄位)。

喜歡這個外掛嗎?想要更多欄位類型和功能嗎?聯絡作者或在github 上參與貢獻

請參見「其他注意事項」部分以獲取使用信息

用法
對象類型

這個外掛的主要思想是擁有一個統一的 API,可以處理任何對象類型。目前,Custom Metadata Manager 可以使用在 user、comment 和任何內建或自訂的文章類型,例如: post、page 等。

註冊你的自訂欄位

為了避免潛在的競爭條件和提高效能,在 admin_menu 鉤子中註冊你的自訂欄位。這樣你的前端就不會因為不必要的處理而變慢,並且可以確保你的欄位可以被安全註冊。下面是一個程式碼範例:

add_action('admin_menu', 'my_theme_init_custom_fields');

function my_theme_init_custom_fields(){
if(function_exists('x_add_metadata_field') && function_exists('x_add_metadata_group')){
x_add_metadata_field('my_field', array('user','post'));
}
}

獲取資料

你可以像平常一樣使用 get_metadata 函式來獲取資料。Custom Metadata Manager 使用你提供的 slug 名稱,使用 WordPress 的 metadata API 來儲存所有資料。因此,即使你決定停用這個外掛,你的資料也是安全的且可以存取。對於選項,你可以使用 get_option。

例如:

$value = get_metadata('post', get_the_ID(), 'featured', true); // 返回欄位「featured」的文章 metadata 值

添加 Metadata 群組

一個群組本質上是一個將多個欄位群組在一起的 metabox。在任何欄位之前註冊這個群組。

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

參數

$slug(字串)資料將儲存的索引鍵。
$object_types(字串|陣列)此欄位應該加入的對象類型。支援:post、page、任何自訂文章類型、user、comment。

選項和覆寫:

$args = array(
'label' => $group_slug, // 群組的標籤
'context' => 'normal', // (僅限 post)
'priority' => 'default', // (僅限 post)

原文外掛簡介

An easy way to add custom fields to your object types (post, pages, custom post types, users) & to generate option pages.
The goal of this plugin is to help you rapidly build familiar, intuitive interfaces for your users in a very WordPress-native way.
The custom field panel is nice, but not quite the easiest thing for users to work with. Adding your own metaboxes and fields involves a lot of time and repetitive code that could be better used elsewhere.
This plugin handles all that heavy-lifting for you behind-the-scenes, so that you can focus on more on building out and connecting your data rather than all the minor details. This single piece of code x_add_metadata_field( 'my-field-name', 'post' ); generates a metabox with a text field inside it, with the necessary hooks to save the entered values.
The API is similar to that used for registering custom post types and taxonomies so it should be familiar territory.
NOTE: The plugin now requires WordPress 3.3+ (chiefly for the wysiwyg & datepicker fields)
Like what you see? Want more field types and features added? Get in touch or contribute on github

See “Other Notes” section for usage information

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 admin_menu 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( 'admin_menu', 'my_theme_init_custom_fields' );

function my_theme_init_custom_fields() {
if( function_exists( 'x_add_metadata_field' ) && function_exists( 'x_add_metadata_group' ) ) {
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 )
)
);
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 );

TODOs
Stuff we have planned for the future:

Ability to clone (multiple) groups of fields
Ability Pass in attributes for built-in fields (e.g. class, data-*, etc.)
Additional field types (multi-select, multi-checkbox)
Limit or exclude groups and fields using a custom callback
Autosave support for fields on post types
Client- and server-side validation support
Add groups and fields to Quick Edit

各版本下載點

  • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
  • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「Custom Metadata Manager」來進行安裝。

(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


0.1 | 0.2 | 0.3 | 0.5 | 0.6 | 0.7 | 0.5.1 | 0.5.2 | 0.5.3 | 0.5.4 | 0.5.5 | 0.5.6 | 0.5.7 | 0.7.1 | trunk |

延伸相關外掛(你可能也想知道)

暫無相關外掛推薦。

文章
Filter
Apply Filters
Mastodon