百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
E

extended-acf

> 编程语言
开源

使用面向对象的 PHP 注册高级自定义字段

511 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

使用面向对象的 PHP 注册高级自定义字段

Extended ACF

Register advanced custom fields with object-oriented PHP.

Extended ACF provides an object-oriented API to register groups and fields with ACF. If you register fields in your theme, you can safely rely on version control when working with other developers. Oh, you don't have to worry about unique field keys.

  • Installation
  • Usage
  • Settings
  • Fields
    • Basic
    • Content
    • Choice
    • Relational
    • Advanced
    • Layout
  • Location
  • Conditional Logic
  • Bidirectional Relationships
  • Non-standards
    • helperText
    • column
    • dd and dump
    • key
    • withSettings
  • Macros
  • Custom Fields
  • Upgrade Guide

Installation

Require this package, with Composer, in the root directory of your project.

bash
composer require vinkla/extended-acf

To install the Advanced Custom Fields Pro plugin, download and place it in either the plugins or mu-plugins directory. After that, activate the plugin in the WordPress dashboard.

Learn more about installing ACF PRO using Composer.

Usage

To register a new field group, use the register_extended_field_group() function. This extends the default register_field_group() function from the ACF plugin. The key value is appended to field groups and fields. Here's an example of a field group.

php
use Extended\ACF\Fields\Image;
use Extended\ACF\Fields\Text;
use Extended\ACF\Location;

add_action('acf/include_fields', function() {
    register_extended_field_group([
        'title' => 'About',
        'fields' => [
            Image::make('Image'),
            Text::make('Title'),
        ],
        'location' => [
            Location::where('post_type', 'page')
        ],
    ]);
});

Settings

For detailed information on field group settings, please consult the official ACF documentation. You can also find more examples in the examples directory.

  • Register custom post type
  • Register custom post type with Extended CPT
  • Register Gutenberg block
  • Register options page

Fields

All fields, except the clone field, have a corresponding class. Each field needs a label. If no name is specified, the label will be used as the name in snake_case. The name can only contain alphanumeric characters and underscores.

php
use Extended\ACF\Fields\Text;

Text::make('Title', 'heading')
    ->helperText('Add the text value')
    ->required()

Most fields have the methods default, required, and wrapper. The basic fields also have the methods prepend, append, placeholder, readOnly, and disabled. Please also check the non-standard methods mentioned in the non-standards section.

Basic

Email - The email field creates a simple email input.

php
use Extended\ACF\Fields\Email;

Email::make('Email')
    ->helperText('Add the employees email address.')
    ->required()

Number - The number field creates a simple number input.

php
use Extended\ACF\Fields\Number;

Number::make('Age')
    ->helperText('Add the employees age.')
    ->min(18)
    ->max(65)
    ->required()

Password - The password field creates a simple password input.

php
use Extended\ACF\Fields\Password;

Password::make('Password')
    ->helperText('Add the employees secret pwned password.')
    ->required()

Range - The range field provides an interactive experience for selecting a numerical value.

php
use Extended\ACF\Fields\Range;

Range::make('Rate')
    ->helperText('Add the employees completion rate.')
    ->min(0)
    ->max(100)
    ->step(10)
    ->required()

Text - The text field creates a simple text input.

php
use Extended\ACF\Fields\Text;

Text::make('Name')
    ->helperText('Add the employees name.')
    ->maxLength(100)
    ->required()

Textarea - The textarea field creates a simple textarea.

php
use Extended\ACF\Fields\Textarea;

Textarea::make('Biography')
    ->helperText('Add the employees biography.')
    ->newLines('br') // br, wpautop
    ->maxLength(2000)
    ->rows(10)
    ->required()

URL - The URL field creates a simple uRL input.

php
use Extended\ACF\Fields\URL;

URL::make('Website')
    ->helperText('Add the employees website link.')
    ->required()

Content

File - The file field allows a file to be uploaded and selected.

php
use Extended\ACF\Fields\File;

File::make('Resturant Menu', 'menu')
    ->helperText('Add the menu **pdf** file.')
    ->acceptedFileTypes(['pdf'])
    ->library('all') // all, uploadedTo
    ->minSize('400 KB')
    ->maxSize(5) // MB if entered as int
    ->format('array') // id, url, array (default)
    ->required()

Gallery - The gallery field provides a simple and intuitive interface for managing a collection of images.

php
use Extended\ACF\Fields\Gallery;

Gallery::make('Images')
    ->helperText('Add the gallery images.')
    ->acceptedFileTypes(['jpg', 'jpeg', 'png'])
    ->minHeight(500)
    ->maxHeight(1400)
    ->minWidth(1000)
    ->maxWidth(2000)
    ->minFiles(1)
    ->maxFiles(6)
    ->minSize('400 KB')
    ->maxSize(5) // MB if entered as int
    ->library('all') // all, uploadedTo
    ->format('array') // id, url, array (default)
    ->previewSize('medium') // thumbnail, medium, large
    ->prependFiles()
    ->required()

Image - The image field allows an image to be uploaded and selected.

php
use Extended\ACF\Fields\Image;

Image::make('Background Image')
    ->helperText('Add an image in at least 12000x100px and only in the formats **jpg**, **jpeg** or **png**.')
    ->acceptedFileTypes(['jpg', 'jpeg', 'png'])
    ->minHeight(500)
    ->maxHeight(1400)
    ->minWidth(1000)
    ->maxWidth(2000)
    ->minSize('400 KB')
    ->maxSize(5) // MB if entered as int
    ->library('all') // all, uploadedTo
    ->format('array') // id, url, array (default)
    ->previewSize('medium') // thumbnail, medium, large
    ->required()

Oembed - The oEmbed field allows an easy way to embed videos, images, tweets, audio, and other content.

php
use Extended\ACF\Fields\Oembed;

Oembed::make('Tweet')
    ->helperText('Add a tweet from Twitter.')
    ->required()

WYSIWYG - The WYSIWYG field creates a full WordPress tinyMCE content editor.

php
use Extended\ACF\Fields\WYSIWYGEditor;

WYSIWYGEditor::make('Content')
    ->helperText('Add the text content.')
    ->tabs('visual') // all, text, visual (default)
    ->toolbar(['bold', 'italic', 'link']) // aligncenter, alignleft, alignright, blockquote, bold, bullist, charmap, forecolor, formatselect, fullscreen, hr, indent, italic, link, numlist, outdent, pastetext, redo, removeformat, spellchecker, strikethrough, underline, undo, wp_adv, wp_help, wp_more
    ->disableMediaUpload()
    ->lazyLoad()
    ->required()

Choice

Button Group - The button group field creates a list of radio buttons.

php
use Extended\ACF\Fields\ButtonGroup;

ButtonGroup::make('Color')
    ->helperText('Select the box shadow color.')
    ->choices(['Forest Green', 'Sky Blue']) // ['forest_green' => 'Forest Green', 'sky_blue' => 'Sky Blue']
    ->default('forest_green')
    ->format('value') // array, label, value (default)
    ->required()

Checkbox - The checkbox field creates a list of tick-able inputs.

php
use Extended\ACF\Fields\Checkbox;

Checkbox::make('Color')
    ->helperText('Select the border color.')
    ->choices(['Forest Green', 'Sky Blue']) // ['forest_green' => 'Forest Green', 'sky_blue' => 'Sky Blue']
    ->default('forest_green')
    ->format('value') // array, label, value (default)
    ->layout('horizontal') // vertical, horizontal
    ->create(save: true) // allow custom values, save them to the choices
    ->toggle() // add a toggle all checkbox
    ->required()

Radio Button - The radio button field creates a list of select-able inputs.

php
use Extended\ACF\Fields\RadioButton;

RadioButton::make('Color')
    ->helperText('Select the text color.')
    ->choices(['Forest Green', 'Sky Blue']) // ['forest_green' => 'Forest Green', 'sky_blue' => 'Sky Blue']
    ->default('forest_green')
    ->format('value') // array, label, value (default)
    ->create(save: true) // add an other choice, save custom values to the choices
    ->required()

Select - The select field creates a drop down select or multiple select input.

php
use Extended\ACF\Fields\Select;

Select::make('Color')
    ->helperText('Select the background color.')
    ->choices(['Forest Green', 'Sky Blue']) // ['forest_green' => 'Forest Green', 'sky_blue' => 'Sky Blue']
    ->default('forest_green')
    ->format('value') // array, label, value (default)
    ->multiple()
    ->nullable()
    ->stylized() // stylized checkbox using select2
    ->lazyLoad() // use AJAX to lazy load choices
    ->create(save: true) // allow creation of new options, save them to the choices
    ->required()

True False - The true / false field allows you to select a value that is either 1 or 0.

php
use Extended\ACF\Fields\TrueFalse;

TrueFalse::make('Social Media', 'display_social_media')
    ->helperText('Select whether to display social media links or not.')
    ->default(false)
    ->stylized(on: 'Yes', off: 'No') // optional on and off text labels
    ->required()

Relational

Link - The link field provides a simple way to select or define a link (url, title, target).

php
use Extended\ACF\Fields\Link;

Link::make('Read More Link')
    ->format('array') // url, array (default)
    ->required()

Page Link - The page link field allows the selection of 1 or more posts, pages or custom post types.

php
use Extended\ACF\Fields\PageLink;

PageLink::make('Contact Link')
    ->postTypes(['contact'])
    ->postStatus(['publish']) // draft, future, pending, private, publish
    ->taxonomies(['category:city'])
    ->disableArchives()
    ->nullable()
    ->multiple()
    ->required()

Post Object - The post object field creates a select field where the choices are your pages +

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPacfadvanced-custom-fieldscomposerwordpress

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言