[WordPress] 外掛分享: Easy Custom Fields

首頁外掛目錄 › Easy Custom Fields
60+
安裝啟用
尚無評分
4997 天前
最後更新
問題解決
WordPress 2.9.2+ v0.6 上架:2010-05-01

內容簡介

功能:

使用多個欄位 / 群組簡單生成文章框
易於驗證 / 清理輸入和輸出數據
通過 $easy_cf->field_id->get() 或 $easy_cf->field_id->get(NULL,$raw=true) 輕鬆訪問欄位數據
通過管理員通知獲取驗證失敗的錯誤消息
自定義文章類型感知
通過擴展 Easy_CF_Field 和 Easy_CF_Validator 類來擴展您的需求(請參閱高級用法)

由於此腳本主要作為開發人員的基礎,因此需要一些編程技能才能為您的主題添加此功能。

為了使用這個類,只需按照下面的說明從您的主題的 functions.php 文件中初始化它即可。

簡單用法
require_once( WP_PLUGIN_DIR . '/easy-custom-fields/easy-custom-fields.php' );
$field_data = array (
'testgroup' => array ( // 唯一的群組 ID
'fields' => array( // 拥有字段定义的字段数组
'field1' => array(), // 全局唯一的字段 ID
'field2' => array(),
'field3' => array(),
),
),
);
$easy_cf = new Easy_CF($field_data);

高級用法
require_once( WP_PLUGIN_DIR . '/easy-custom-fields/easy-custom-fields.php' );
$field_data = array (
'testgroup' => array (
'fields' => array(
'field1' => array(),
'field2' => array(),
'field3' => array(),
),
),
'advanced_testgroup' => array ( // 唯一的群組 ID
'fields' => array( // 拥有字段定义的字段数组
'advanced_field' => array( // 全局唯一的字段 ID
'label' => '進階字段描述', // 字段標籤
'hint' => '長進階字段描述', // 字段的一個描述性提示
'type' => 'textarea', // 自訂欄位類型(見參考:field_type)
'class' => 'aclass', // CSS 封裝器類
'input_class' => 'theEditor', // 輸入欄位的 CSS 類
'error_msg' => '進階字段出現問題', // 驗證失敗時顯示的錯誤消息
'validate' => 'validatorname', // 自定義驗證器(見參考:validator)
'advanced_email' => array(
'label' => '電子郵件',
'hint' => '輸入您的電子郵件地址',
'validate' => 'email', )
),
'title' => '產品描述', // 群組標題
'context' => 'advanced', // 與 https://codex.wordpress.org/Function_Reference/add_meta_box 中一致的上下文
'pages' => array( 'post', 'page' ), // 與 https://codex.wordpress.org/Function_Reference/add_meta_box 中相同的頁面
),
);

if ( !class_exists( "Easy_CF_Validator_Email" ) ) {

class Easy_CF_Validator_Email extends Easy_CF_Validator {
public function get( $value='' ) {
return esc_attr( $value );
}

public function set( $value='' ) {
$value = esc_attr( trim( stripslashes( $value ) ) );
return $value;
}

public function validate( $value='' ) {

外掛標籤

開發者團隊

⬇ 下載最新版 (v0.6) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Easy Custom Fields」→ 直接安裝(推薦)

原文外掛簡介

Features:

simply generate post boxes with multiple fields / groups
easily validate/sanitize input and output data
easy access to field data via $easy_cf->field_id->get() or $easy_cf->field_id->get( NULL, $raw=true );
get error messages for validation failures via admin notices
custom post type aware
extendable to your needs by extending Easy_CF_Field and Easy_CF_Validator classes (see advanced usage)

As this script is mainly meant as basis for developers it needs minor coding skills to add this functionality
to your theme.
In order to make use of this class simply initialize it from the functions.php file of your theme as described below.
Simple Usage
require_once( WP_PLUGIN_DIR . '/easy-custom-fields/easy-custom-fields.php' );
$field_data = array (
'testgroup' => array ( // unique group id
'fields' => array( // array "fields" with field definitions
'field1' => array(), // globally unique field id
'field2' => array(),
'field3' => array(),
),
),
);
$easy_cf = new Easy_CF($field_data);

Advanced Usage
require_once( WP_PLUGIN_DIR . '/easy-custom-fields/easy-custom-fields.php' );
$field_data = array (
'testgroup' => array (
'fields' => array(
'field1' => array(),
'field2' => array(),
'field3' => array(),
),
),
'advanced_testgroup' => array ( // unique group id
'fields' => array( // array "fields" with field definitions
'advanced_field' => array( // globally unique field id
'label' => 'Advanced Field Description', // Field Label
'hint' => 'Long Advanced Field description', // A descriptive hint for the field
'type' => 'textarea', // Custom Field Type (see Ref: field_type)
'class' => 'aclass', // CSS Wrapper class for the field
'input_class' => 'theEditor', // CSS class for the input field
'error_msg' => 'The Advanced Field is wrong' ), // Error message to show when validate fails
'validate' => 'validatorname', // Custom Validator (see Ref: validator)
'advanced_email' => array(
'label' => 'Email',
'hint' => 'Enter your email',
'validate' => 'email', )
),
'title' => 'Product Description', // Group Title
'context' => 'advanced', // context as in https://codex.wordpress.org/Function_Reference/add_meta_box
'pages' => array( 'post', 'page' ), // pages as in https://codex.wordpress.org/Function_Reference/add_meta_box
),
);

if ( !class_exists( "Easy_CF_Validator_Email" ) ) {

class Easy_CF_Validator_Email extends Easy_CF_Validator {
public function get( $value='' ) {
return esc_attr( $value );
}

public function set( $value='' ) {
$value = esc_attr( trim( stripslashes( $value ) ) );
return $value;
}

public function validate( $value='' ) {
if ( empty( $value ) || is_email( $value ) )
return true;
else
return false;
}
}
}

if ( !class_exists( "Easy_CF_Field_Textarea" ) ) {
class Easy_CF_Field_Textarea extends Easy_CF_Field {
public function print_form() {
$class = ( empty( $this->_field_data['class'] ) ) ? $this->_field_data['id'] . '_class' : $this->_field_data['class'];
$input_class = ( empty( $this->_field_data['input_class'] ) ) ? $this->_field_data['id'] . '_input_class' : $this->_field_data['input_class'];

$id = ( empty( $this->_field_data['id'] ) ) ? $this->_field_data['id'] : $this->_field_data['id'];
$label = ( empty( $this->_field_data['label'] ) ) ? $this->_field_data['id'] : $this->_field_data['label'];
$value = $this->get();
$hint = ( empty( $this->_field_data['hint'] ) ) ? '' : '

' . $this->_field_data['hint'] . '

';

$label_format =
'

'.
'

'.
'

'.
'%s'.
'

';
printf( $label_format, $class, $id, $label, $input_class, $id, $value, $hint );
}
}
}

$easy_cf = new Easy_CF($field_data);

Note
If you’re not using auto_init then meta boxes need to be added individually using
add_meta_box( $group_id, $group_title, array( &$easy_cf, ‘meta_box_cb’ ), $page, $group_context );
and the save methods need to be initialized after adding all meta boxes using
$easy_cf->add_save_method();

文章
Filter
Apply Filters
Mastodon