[WordPress] 外掛分享: SuperCPT

前言介紹

  • 這款 WordPress 外掛「SuperCPT」是 2013-02-09 上架。
  • 目前有 800 個安裝啟用數。
  • 上一次更新是 2013-08-10,距離現在已有 4284 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 3.0 以上版本才可以安裝。
  • 有 14 人給過評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

mboynes | unionstreetmedia |

外掛標籤

cms | custom field | custom fields | custom post type | custom post types |

內容簡介

CPT with a string for the taxonomy name. For example,

$genre = new Super_Custom_Taxonomy( 'genre' );

It works a lot like register_taxonomy. The first thing you gained by using this is that the labels all got setup with either ‘Genre’ or ‘Genres’. If our taxonomy were ‘category’, the labels would have “Category” and “Categories” as appropriate. Of course, you do have the ability to set the plural word in cases such as goose/geese. You also gained the ability to define your own custom taxonomy defaults through a filter. Lastly, you gained access to the Super_Custom_Taxonomy‘s parent class, Super_Custom_Post_Meta, for fast, clean, intuitive custom post meta, which we’ll go into shortly.

SuperCPT comes with 361 gorgeous icons courtesy of Font Awesome that are extremely easy to implement. Here’s what it looks like:

$genre->set_icon( 'music' );

Custom Post Meta

Right from the start, SuperCPT was designed to make custom post meta as easy as possible. A lot of effort has gone into making it lean and intuitive. Here are the steps to create an input field:

$contact->add_meta_box( 'Contact Info', array(
'email' => array(
'type' => 'email',
'label' => 'Email Address'
)
));

And that’s it! SuperCPT does the rest. Now, it would spit out a box with a single email field. That email address would be saved as the post meta with a key of “email”. This value is sanitized and validated where necessary (e.g. email addresses and integers). Not shown above are the multitude of configuration options and field types available.

Complete Example

Here’s a quick example of everything SuperCPT has to offer:

// Define a new CPT called ‘Books’ and its taxonomy ‘Genres’.
$books = new Super_Custom_Post_Type( 'book', 'Book' );
$genres = new Super_Custom_Taxonomy( 'genre', 'Genre', 'Genres' );
$books->add_taxonomy( 'genre' );

// Define some custom meta boxes
$books->add_meta_box( array(
'id' => 'book-info',
'context' => 'side',
'fields' => array(
'author' => array( 'type' => 'text', 'label' => 'Author' ),
'published_date' => array( 'type' => 'datepicker', 'label' => 'Published Date' ),
'number_of_pages' => array( 'type' => 'number', 'label' => 'Number of Pages' )
)
));
$books->add_meta_box( array(
'id' => 'book-store',
'title' => 'Store Inventory',
'context' => 'normal',
'fields' => array(
'store_1' => array( 'type' => 'text', 'label' => 'Store 1' ),
'store_2' => array( 'type' => 'text', 'label' => 'Store 2' ),
'store_3' => array( 'type' => 'text', 'label' => 'Store 3' )
)
));

// Set some custom icons
$books->set_icon( 'book' );
$genres->set_icon( 'tags' );

And here’s a screenshot of what the CPT looked like in WordPress:

原文外掛簡介

UPGRADE NOTICE
SuperCPT now uses Font Awesome instead of Glyphicons. Some icon names will carry over, but not all. If this is a considerable inconvenience for you for a project, simply do not update it. It’s not a security release, so updating isn’t necessary.
Overview
SuperCPT is an object wrapper for Custom Post Types, Custom Taxonomies, and Custom Post Meta “for coders, by coders.” Simply put, SuperCPT:

DRYs up the Custom Post Type and Custom Taxonomy process (e.g. automatically adds the name to all the labels),
allows you to set default options for all your Custom Post Types and Taxonomies,
significantly simplifies the process of creating, saving, and displaying Custom Post Meta,
is sexy! Your custom fields are styled to look great and SuperCPT comes with 361 awesome icons courtesy of Font Awesome.

Demo Video

TextMate/Sublime Text 2 Bundle
If you use TextMate, Sublime Text 2, or another editor which supports TextMate bundles, check out this set of snippets to turbo-charge your development.
And more…
See the Other Notes tab for instructions and demo code. Find more demos and a full reference at GitHub.
Since you’re a hard-core coder, check this out on GitHub if you want to contribute!
Instructions
Depending on when and where you’re declaring your Custom Post Types and Taxonomies, you have different options for which action to hook onto. after_setup_theme is the safest bet, but if you’re referencing this in another plugin, plugins_loaded is a good choice. To avoid a fatal error if something goes awry, you should check to see if the class Super_Custom_Post_Type exists before referencing it. Don’t worry about keeping up, reference code is below.
Custom Post Types
To define a new Custom Post Type, instantiate the Super_Custom_Post_Type class with a string for the post type. For example,
$movies = new Super_Custom_Post_Type( 'movie' );

It works very much like register_post_type. The first thing you gained by using this is that the labels all got setup with either ‘Movie’ or ‘Movies’. If our post type were ‘indie-film’, the labels would have “Indie Film” and “Indie Films” as appropriate. Of course, you do have the ability to set the plural word in cases such as goose/geese. You also gained the ability to define your own custom post type defaults through a filter. Lastly, you gained access to Super_Custom_Post_Type‘s parent class, Super_Custom_Post_Meta, for fast, clean, intuitive custom post meta, which we’ll go into shortly.
Lastly, if you’ve built a lot of custom post types, you’re probably sick and tired of the pushpin icon. SuperCPT comes with 350 gorgeous icons courtesy of Font Awesome that are extremely easy to implement. Here’s what it looks like:
$movies->set_icon( 'film' );

Custom Taxonomies
To define a new Custom Taxonomy, much like with Custom Post Types, you instantiate Super_Custom_Taxonomy with a string for the term name. For example:
$actors = new Super_Custom_Taxonomy( 'actor' );

Again, we got free labels for doing this, using either ‘Actor’ or ‘Actors’ as appropriate, without needing to specify the 16 labels individually.
Custom Post Meta
Custom Post Meta is where SuperCPT shines the brightest, because this process is typically the most time-consuming. Super_Custom_Post_Meta is a free-standing class that can be added to any post type, even built-in post types (posts and pages). This class has a method add_meta_box which does the bulk of the work, and somewhat mimics the WordPress function. Here’s an example:
$movies->add_meta_box( array(
'id' => 'features',
'fields' => array(
'tagline' => array( 'type' => 'text' )
)
) );

The method add_meta_box takes an array of parameters (unlike the core function which takes normal ordered arguments). id is the only required attribute, and that becomes the ID of the meta box as well as the title (this will get converted into “words” for the title, e.g. "movie_details" would become “Movie Details”). fields is an array of all the fields in the meta box. It’s an associative array, where the keys in the array are the field names and the values are another associative array of attributes for the field. The keys closely reflect the HTML attributes in the resulting field, and any key not known by the plugin will in fact become an HTML attribute (e.g. passing 'data-src' => 'foo' would become the HTML attribute data-src="foo" in the field). See the reference for the full gamut of options, both for the add_meta_box argument array and the fields array.
Long story short, using this class means you don’t have to do any additional work to store data, retrieve data, style the boxes, and so on.
Helper Functions
SuperCPT has a couple of helper functions for displaying your post meta. get_scpt_formatted_meta and the_scpt_formatted_meta
Demo Code
Here is the full demo code:
function scpt_demo() {
if ( ! class_exists( 'Super_Custom_Post_Type' ) )
return;

$demo_posts = new Super_Custom_Post_Type( 'demo-post' );

# Test Icon. Should be a square grid.
$demo_posts->set_icon( 'th-large' );

# Taxonomy test, should be like tags
$tax_tags = new Super_Custom_Taxonomy( 'tax-tag' );

# Taxonomy test, should be like categories
$tax_cats = new Super_Custom_Taxonomy( 'tax-cat', 'Tax Cat', 'Tax Cats', 'category' );

# Connect both of the above taxonomies with the post type
connect_types_and_taxes( $demo_posts, array( $tax_tags, $tax_cats ) );

# Add a meta box with every field type
$demo_posts->add_meta_box( array(
'id' => 'demo-fields',
'context' => 'normal',
'fields' => array(
'textbox-demo' => array(),
'textarea-demo' => array( 'type' => 'textarea' ),
'wysiwyg-demo' => array( 'type' => 'wysiwyg' ),
'boolean-demo' => array( 'type' => 'boolean' ),
'checkboxes-demo' => array( 'type' => 'checkbox', 'options' => array( 'one', 'two', 'three' ) ),
'radio-buttons-demo' => array( 'type' => 'radio', 'options' => array( 'one', 'two', 'three' ) ),
'select-demo' => array( 'type' => 'select', 'options' => array( 1 => 'one', 2 => 'two', 3 => 'three' ) ),
'multi-select-demo' => array( 'type' => 'select', 'options' => array( 'one', 'two', 'three' ), 'multiple' => 'multiple' ),
'date-demo' => array( 'type' => 'date' ),
'label-override-demo' => array( 'label' => 'Label Demo' )
)
) );

# Add another CPT to test one-to-one (it could just as easily be one-to-many or many-to-many)
$linked_posts = new Super_Custom_Post_Type( 'linked-post', 'Other Post', 'Other Posts' );
$linked_posts->add_meta_box( array(
'id' => 'one-to-one',
'title' => 'Testing One-to-One relationship',
'context' => 'side',
'fields' => array(
'demo-posts' => array( 'type' => 'select', 'data' => 'demo-post' ),
'side-wysiwyg' => array( 'type' => 'wysiwyg' )
)
) );
$linked_posts->set_icon( 'cogs' );

}
add_action( 'after_setup_theme', 'scpt_demo' );

To-Do

1. Add better support for multiple fields for one meta key
2. Add easy RSS feeds, e.g. in fields array, a parameter might be 'rss' => 'PubDate' to prefer that field’s data over the post’s publication date.

各版本下載點

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

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


0.1 | 0.2 | 0.1.1 | 0.1.2 | 0.1.3 | 0.2.1 | trunk |

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

  • Advanced Custom Fields (ACF®) 》Advanced Custom Fields 可以讓 WordPress 網站成為一個完整的內容管理系統,提供您所有工具以更好管理您的數據。, 使用 Advanced Custom Fields 外掛,完全...。
  • Meta Box 》Meta Box 是一個強大、專業又輕量級的工具組,供開發者在 WordPress 中為任何自訂文章型別建立自訂的 Meta Box 與自訂欄位。, 透過 Meta Box,您可以使用超過...。
  • Checkout Field Editor (Checkout Manager) for WooCommerce 》WooCommerce 結帳欄位編輯器外掛, WooCommerce 結帳欄位編輯器 (Checkout Manager) 外掛讓您可以在結帳頁面上添加 20 種不同類型的自訂欄位,並搭配直覺且易...。
  • Pods – Custom Content Types and Fields 》使用「Pods Framework」在一個地方管理你所有自訂內容需求。, , , 建立內容類型,包括自訂文章類型、自訂分類、以及我們專屬的「進階內容類型」(ACT),...。
  • Flexible Checkout Fields for WooCommerce – WooCommerce Checkout Manager 》我們認為 WooCommerce 是 WordPress 最好的電子商務外掛,但它缺乏一些非常基本的功能,例如使用易於使用的接口自定義結帳管理器來自訂結帳欄位。您可以使用 ...。
  • Advanced Custom Fields: Extended 》🚀 全方位增強套件,可改進WordPress和Advanced Custom Fields。此外掛旨在提供一個強大的管理框架,涵蓋了眾多改進和優化。, 此外掛需要至少 ACF Pro 5.8。,...。
  • Sydney Toolbox 》Sydney Toolbox 外掛只適用於 Sydney WordPress 主題。, 此外掛註冊了在 Sydney 主題 中所需的自定義文章類型和自定義欄位。。
  • Custom Field Suite 》Custom Field Suite (CFS) 讓您為文章添加自定義欄位。它輕量級且經過過往使用者的測試(很難出現錯誤)。, 需要了解的事項, , 我們不提供支援。, 這是一個免...。
  • Custom Field Template 》「Custom Field Template 外掛」在「編寫文章/頁面」時,新增自訂欄位的預設設定。這個模板格式基本上跟「rc:custom_field_gui 外掛」差不多,不同處在於:, ...。
  • Custom Post Types and Custom Fields creator – WCK 》WordPress Creation Kit 是由三個工具組成,可幫助您建立和維護自訂文章類型、自訂分類和最重要的是文章、頁面或自訂文章類型的自訂欄位和元框。, WCK Custom...。
  • Secure Custom Fields 》總結:SCF 是一款能夠擴展 WordPress 功能的外掛,使其成為一個靈活的內容管理工具。透過 SCF,管理自定義數據變得簡單高效。, , 1. 為什麼 SCF 是一個靈活的...。
  • JSM Show Post Metadata 》JSM's Show Post Metadata外掛會在文章編輯頁面底部顯示文章(例如文章、頁面和自訂文章類型)的meta key(也就是自訂欄位名稱)和它們的非序列化值。, ...。
  • Advanced Custom Fields: Nav Menu Field 》使用 Nav Menu Field 外掛將導覽選單加入Advanced Custom Fields (ACF)。此外掛會在 ACF(版本 5 和 4)加入 Nav Menu 欄位類型,讓您從 WordPress 管理後台...。
  • Frontend Admin by DynamiApps 》這個外掛讓你可以在前端編輯及新增文章、頁面、使用者、專有名詞欄位等等。 (之前稱為 ACF Frontend), 這個外掛允許你在網站上顯示前端管理表單,讓使用者可...。
  • Simple CSV/XLS Exporter 》本外掛可以讓您透過簡單的連結/按鈕,從後端或前端匯出文章為 CSV 或 XLS 檔案。, 請確保您使用 PHP 7.3 或更新版本,如果您看到任何錯誤,舊版本將不再受支...。

文章
Filter
Apply Filters
Mastodon