• Stars
    star
    128
  • Rank 272,237 (Top 6 %)
  • Language
    PHP
  • Created over 6 years ago
  • Updated about 1 month ago

Reviews

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

Repository Details

A wrapper class to help write more readable ACF field declarations.

geniem-github-banner

ACF Codifier

Description

A helper class to make defining ACF field groups and fields easier in the code.

A complete documentation of the classes can be found here.

Installation

The recommended way to install ACF Codifier is by Composer:

$ composer require devgeniem/acf-codifier

OR add it in your composer.json:

{
  "require": {
    "devgeniem/acf-codifier": "*"
  }
}

Installing the plugin with Composer requires Bedrock's autoloader. It installs as an mu-plugin and doesn't need to be activated.

You can, however, install it also as an ordinary plugin. It can be done in two ways:

  • Clone this repository into your plugins directory and run composer install --no-dev in the repository folder.

  • Download the latest release here and just extract the archive in your plugins directory.

Usage

All classes of the Codifier are under the namespace Geniem\ACF. For easiness, it's better to put your declarations in separate file(s) and declare namespace Geniem\ACF; on top of them. Rest of this ReadMe supposes you have done that.

Creating a field group.

Field groups live in a class called Group. New group is thus created with:

$field_group = new Group( 'Field Group' );

This will create a new field group with Field Group as its title and field-group as the key.

If you want to define the key yourself, you can either give it as the second parameter to the constructor or use:

$field_group->set_key( 'new_key' );

You can also change the title of the group later with set_title() method.

There are methods for defining all the other properties of a field group as well. All the field group commands can be chained. For example:

$field_group->set_position( 'side' )         // Set the field group to be shown in the side bar of the edit screen.
            ->set_style( 'seamless' )        // Set the field group to show as seamless.
            ->hide_element( 'the_content' ); // Hide the native WP content field.

Very rarely anyone wants their field group to be shown in every edit screen of a WordPress installation. The visibility rules are handled with their own class RuleGroup. A rule group is created and linked with a group like this:

$rule_group = new RuleGroup();
$rule_group->add_rule( 'post_type', '==', 'page' );

$field_group->add_rule_group( $rule_group );

You can add multiple rules to a rule group, and each rule within a group is considered an 'and'. If you add multiple rule groups to a field group, they are considered an 'or'.

Field group is registered to use with register method:

$field_group->register();

Obviously your new field group wouldn't have any fields at this point, but don't worry, we get to them later.

Comprehensive documentation of the class can be found here.

Creating fields

Like field groups, fields are also objects of their own. They live in classes named for their field types. For example a text field can be created with:

$text = new Field\Text( 'Text field' );

Now the $text variable is populated with a text field with Text field as its label and text-field with both as its key and its name.

The key and the name can also be given to the constructor as its second and third parameters respectively. Obviously there are set_key() and set_name() methods also available like there were with the groups as well.

The plugin checks that the field key is unique within the project and triggers a notice if there is a collision.

Every property a field type has is defined with its own method. Like the field groups, they can be chained with the fields as well.

$text->set_placeholder( 'Placeholder text' ) // Set a placeholder text.
     ->set_append( 'Appendable' )            // Set an appending text.
     ->set_maxlength( 30 );                  // Set the maxlength.

ACF's conditional logic groups work very similarly to groups' location rules. First you need to create an object from ConditionalLogicGroup and add the rule there:

$conditional_logic = new ConditionalLogicGroup();
$conditional_logic->add_rule( 'another_field', '==', true );

$text->add_conditional_logic( $conditional_logic );

The logic between 'ands' and 'ors' is the same than it is with the groups' location rules.

Fields are added to field groups with the add_field method:

$field_group->add_field( $text );

Normally add_field adds the field to the end of the field group. If you want the field to be inserted first, append a second parameter with first as its value:

$field_group->add_field( $text, 'first' );

You can also insert the field into the field group after or before another field with following methods:

$field_group->add_field_before( $text, 'target_field_key' );
$field_group->add_field_after( $text, $target_field_object );

You can use either the field key or the field object with both methods.

There are also methods like add_fields() that can be used to add an array of fields at once, and add_fields_from() that takes another groupable object (for example a field group, group field, repeater or a flexible layout) as its first parameter and copies its fields to the calling object.

$field_group->add_fields_from( $repeater );

List of all field types and their methods can be found here.

Grouping field types

There are several special field types that can have subfields within them.

Group and repeater

The group and the repeater fields are the simplest of the grouping field types. They are very straightforward:

$group = new Field\Group( 'Field name' );

$group->set_layout( 'table' )
      ->add_field( $some_field )
      ->add_field( $another_field );

$field_group->add_field( $group );
Flexible content

Flexible content fields consist of layouts which contain the fields.

$flexible_content = new Field\FlexibleContent( 'Flexible field' );

$layout = new Field\Flexible\Layout( 'Layout label' );

$layout->set_display_mode( 'row' )
       ->add_field( $some_field )
       ->add_field( $another_field );

$flexible_content->add_layout( $layout );

Like fields, layouts can also take key and name as their second and third parameters.

Clone

Clone field is a special case in that its class name is not the same than the field slug. Clone is a reserved word in PHP so the class name of the field is CloneField.

You can clone both fields and field groups, so the field's add_clone() method can take both as a parameter. It can also be given just the key of the desired field or field group as a string.

$clone = new Field\CloneField( 'Clone' );

$clone->set_label_prefix()        // Set label prefix setting as true
      ->add_clone( $some_field )  // Add a field object
      ->add_clone( $some_group )  // Add a field group object.
      ->add_clone( 'field-key' ); // Add a field by its key

$field_group->add_field( $clone );
Tab and Accordion

With ACF Codifier the tab and accordion field types are treated like they had subfields. Otherwise they works just the same as native ACF fields would.

$tab = new Field\Tab( 'My Very First Tab' );

$tab->set_placement( 'left' )
    ->set_endpoint()
    ->add_field( $some_field )
    ->add_field( $another_field );

$field_group->add_field( $tab );
Pseudo group

Pseudo group is like a group field, but it doesn't affect the data tree or the admin view. It only acts as a container for multiple fields, which then appear as independents fields when viewing the edit page or looking at the data tree.

$pseudo = new Field\Pseudo( 'pseudo-group' );

$pseudo->add_field( $some_field )
       ->add_field( $another_field );

Gutenberg

Codifier has a feature to register Gutenberg blocks using ACF's register block feature internally. It works in a very similar fashion than the basic field creation in Codifier as well.

Block's constructor takes two mandatory parameters: the title and the name (or key) of the block. The properties are then set for the block with appropriate methods.

$block = new \Geniem\ACF\Block( 'Some block', 'some_block' );
$block->set_category( 'common' );
$block->add_post_type( 'post' );
$block->set_mode( 'edit' );

The rendering of the block happens with a Renderer class. Codifier includes three renderers by default: CallableRenderer that uses a simple method for rendering; PHP that renders a normal PHP file with the given data and Dust that uses DustPHP templates for rendering.

The following uses the print_r() method to output a list of the data from the fields.

$renderer = new \Geniem\ACF\Renderer\CallableRenderer( function( $data ) {
  return print_r( $data, true );
});

$block->set_renderer( $renderer );

The ACF fields themselves are added to the block as they would to any other groupable type object with methods like add_field() and set_fields().

To register the block for Gutenberg, just use the register() method.

$block->register();

If you need, the abovementioned method returns the output of ACF's register_block() function.

Additional features

Prevent Flexible Content layouts from showing in some post types or page templates

If you want to prevent some Flexible Content layouts from showing in some post types or page templates, you can do so with exclude_post_type or exclude_template methods:

$layout->exclude_post_type( 'post' );
$layout->exclude_template( 'page-frontpage.php' );

There are also set_exclude_post_types and set_exclude_templates methods with which you can set multiple excludes at once with an array.

Hide field label

With the Codifier you can hide a field's label on the admin side. It might be useful for example with flexible content fields or a group field.

You achieve this simply by calling hide_label() for your field.

$field->hide_label();

There are also show_label() and get_label_visibility() methods.

Additional field types

PHP field

The PHP field is an ACF field type that can only be used with the Codifier. It allows the developer to run his own code within the field area in the admin side and print anything between the ordinary fields.

The field type shows up in the ACF admin as well, but there are no functionality that can be used from there.

Usage of the field type is very straightforward. You can just run your own code like this:

$php = new Field\PHP( __( 'My PHP field' ) );
$php->run( function() {
    global $post;

    echo '<pre>';
    print_r( $post );
    echo '</pre>';
});

Multisite Relationship

The Multisite Relationship is an ACF field type that can only be used with the Codifier. It is a clone of the original Relationship field but with the ability to define the blog from which the posts can be picked.

The usage is otherwise exactly the same as with the Relationship field, but there is a new set_blog_id() method.

$ms_relationship = new Field\MultisiteRelationship( __( 'My Multisite Relationship field', 'multisite_relationship', 'multisite_relationship' ) );
$ms_relationship->set_blog_id( 2 );

Multitaxonomy

The Multitaxonomy is an ACF field type that can only be used with the Codifier. It is a clone of the original Taxonomy field but with the ability to define multiple taxonomies to select the terms from. It supports all features defined for the Taxonomy field except the ability to add a new term with the field input. It also has an additional feature for setting the field disabled, which is not possible with the original Taxonomy field.

Usage

Define the field and set the taxonomy slugs to enable selecting terms from multiple taxonomies.

$categories_and_tags = new Field\Multitaxonomy( __( 'Select a category or a tag', 'multitaxonomy_test', 'multitaxonomy_test' ) );
$categories_and_tags->set_taxonomies( [ 'category', 'post_tag' ] );

To enable selecting multiple terms, change the field type to multi_select.

$categories_and_tags->set_field_type( 'multi_select' );

Multisite Taxonomy

The Multisite Taxonomy is an ACF field type that can only be used with the Codifier. It extends the abilities of the Multitaxonomy field by allowing the developer to set a multisite blog id from which the taxonomy terms can be chosen.

Usage

$multisite_taxonomy = new Field\MultisiteTaxonomy( __( 'Select a category or a tag from blog 2', 'multisite_tax', 'multisite_tax' ) );
$multisite_taxonomy->set_taxonomies( [ 'category', 'post_tag' ] );
$multisite_taxonomy->set_blog_id( 2 );

Extended Wysiwyg

The Extended Wysiwyg is an ACF field type that can only be used with the Codifier. It extends the abilities of the Wysiwyg field by allowing the developer to set the height of the TinyMCE editor.

$extended_wysiwyg = new Field\ExtendedWysiwyg( __( 'Extended Wysiwyg', 'extended_wysiwyg', 'extended_wysiwyg' ) );
$extended_wysiwyg->set_height( 150 );

Multisite Post Object

The Multisite Post Object is an ACF field type that can only be used with the Codifier. It is a clone of the original Post Object field but with the ability to define the blog from which the post object can be picked.

The usage is similar to the Post Object field, but there is a new set_blog_id() method for selecting the blog.

$ms_object = new Field\MultisitePostObject( __( 'My Multisite Post Object field', 'multisite_object', 'multisite_object' ) );
$ms_object->set_blog_id( 2 );

Support for external field types

There are also some field types that are created in the ACF Codifier that are not built-in in the ACF itself. These fields require a plugin to work. The plugins should be linked in the docblock comment of the field type class.

If you use some ACF field type plugin, you can either request it to be included in the Codifier by creating an issue on GitHub or creating the field type class yourself and filing a pull request for it.

List of included additional field types

Tips & Tricks

Translations

If translated strings are used as field labels, instructions etc., the Codifier declarations should be run inside an appropriate hook - for example init is fine.

More Repositories

1

dustpress

A WordPress theme framework for writing templates with Dust.js templating engine and separate data models.
PHP
52
star
2

wp-no-admin-ajax

A WordPress plugin that changes the WP AJAX routine and rewrites the ajax requests to custom url rather than admin-ajax.php back-end.
PHP
35
star
3

docker-wordpress

Minimalistic and secure php7+nginx docker container for WordPress
Nginx
33
star
4

redipress

A WordPress plugin that provides a blazing fast search engine and WP Query performance enhancements.
PHP
33
star
5

wp-sanitize-accented-uploads

Replaces accents from future uploads and includes wp-cli command which you can use to sanitize current content.
PHP
25
star
6

wp-geniem-roles

Wrapper classes for developers to create and manipulate WordPress roles.
PHP
17
star
7

ubuntu-docker-wordpress

Base Ubuntu image for WordPress development with nginx and PHP
HTML
15
star
8

wp-geniem-importer

An object-oriented WordPress importer.
PHP
14
star
9

wp-safe-fast-and-clean-collection

This is WordPress plugin and dropin collection which enhances WordPress in multiple ways
11
star
10

better-wp-db-error

Better WordPress db-error.php page with nice wp-cli integration
PHP
11
star
11

wp-queue

WordPress Queue is a modular library for managing queued tasks in WordPress.
PHP
10
star
12

wp-readonly-options

WordPress Plugin to set forced read-only options for get_option()
PHP
9
star
13

acf-required-tabs

An Advanced Custom Fields plugin that adds an indicator for tabs that contain unfilled required fields.
JavaScript
9
star
14

acf-flexible-templates

Create ready made templates for your Flexible Content pages.
PHP
8
star
15

better-wp-install-dropin

Slightly modified WordPress installer.php dropin for cleaner content after installation.
PHP
8
star
16

kubernetes-www-redirect

Kubernetes www. redirect service for bare domains
Shell
7
star
17

tms-theme-base

Tampere WordPress Multisite Base Theme
PHP
7
star
18

wp-multisite-same-level-subdomains

This WordPress multisite plugin allows you to create subsites under the same subdomain depth as the main site.
PHP
7
star
19

docker-openresty-pagespeed

Debian based Nginx container with openresty libraries and pagespeed module included.
6
star
20

wp-oopi

OOPI is a WordPress plugin providing an object-oriented library to ease importing data into WordPress from external sources.
PHP
6
star
21

acf-toggle-flexible-layouts

Plugin to improve ACF flexible layouts usability on WordPress admin side.
JavaScript
6
star
22

docker-alpine-php-base

Baseimage for php containers
5
star
23

docker-webgrind

Docker container for running webgrind xdebug profiler analyzer
PHP
5
star
24

react-native-jigsaw

JavaScript
5
star
25

wp-starter-dustpress-theme

The deprecated DustPress theme boilerplate for Geniem WordPress projects
JavaScript
5
star
26

wp-cron-runner

WP Cron Runner - A simple mu-plugin to run cron jobs on WordPress installations
PHP
5
star
27

wp-gravityforms-db-prevent

A Gravity Forms plugin to let the form creator decide if the values should be saved to database or not.
PHP
5
star
28

wp-pa11y

wp-pa11y helps test sites by going through sitemaps and reports a11y problems
JavaScript
5
star
29

wp-vulnerability-alerts

This plugin scans your system on a daily basis to find vulnerabilities listed in the WPScan Vulnerability Database.
PHP
4
star
30

react-native-safe-storage

Secure storage wrapper for react-native
Java
4
star
31

wp-clean-post-meta-fields

WordPress plugin that keeps your post meta clean from unrelevant custom field data
PHP
4
star
32

silverbullet-kitchensink

A kitchen sink for an isomorphic sails / react application
CSS
4
star
33

wp-redis-group-cache

A plugin extending the Redis Object Cache for WordPress with a group cache functionality.
PHP
4
star
34

typescript-rest-starter

RESt api boilerplate with TypesScript
TypeScript
4
star
35

tms-theme-muumimuseo

Tampere Multisite Theme For Muumimuseo
PHP
3
star
36

wp-security-collection

Composer metapackage which includes WordPress plugins for security.
3
star
37

dustpress-debugger

WordPress plugin that provides handy ajaxified debugger tool for DustPress based themes.
JavaScript
3
star
38

docker-node-assets-builder

Webpack docker container for building assets
Shell
3
star
39

dustpress-js

A DustPress plugin that provides a handy JavaScript library for using your DustPress Model methods on the front end.
JavaScript
3
star
40

tms-plugin-network-site-list

PHP
2
star
41

docker-wordpress-newrelic

Docker image with newrelic monitoring
2
star
42

personaldataflow

Java
2
star
43

wp-core-fixes

WordPress plugin which contains small quick fixes for bugs in WordPress Core
PHP
2
star
44

wp-geniem-project-bells-and-whistles

Geniem WP Project Bells & Whistles
PHP
2
star
45

ubuntu-docker-wordpress-development

Docker Wordpress Image for Development
Dockerfile
2
star
46

geniem-rules-codesniffer

Geniem (WordPress) PHP_CodeSniffer Rules
PHP
2
star
47

ubuntu-docker-openresty-pagespeed

Dockerfile
2
star
48

acf-flexible-icons

Add icons for your ACF Flexible Content layouts.
JavaScript
2
star
49

wp-seo-collection

WordPress SEO related composer packages
2
star
50

wp-cookie-notice

WordPress plugin for displaying "We use cookies" text.
JavaScript
2
star
51

flynn-s3-backup

Docker image for Flynn cluster backup into AWS S3 which you can run as Flynn app
Shell
2
star
52

tms-theme-filharmonia

Tampere Multisite Theme For Tampere Filharmonia
PHP
2
star
53

dustpress-starter-theme

A starter theme for DustPress
PHP
2
star
54

bulmally

An accessibility ready frontend component framework for Bulma CSS framework
HTML
2
star
55

acf-flexible-visibility

A WordPress and an Advanced Custom Fields plugin that adds a Reusable Field Group to the ACF. The field allows the developer to include a field group within another field group.
PHP
2
star
56

docker-wordpress-project-builder

Docker Image for building & testing WordPress projects. This helps to reduce stuff from production containers.
2
star
57

tms-plugin-news-importer

SCSS
2
star
58

wp-admin-only-theme

Super minimalistic theme for sites which only need /wp-admin/ dashboard.
PHP
2
star
59

pipeline-perftest

Performance tests for Geniem Pipeline
JavaScript
1
star
60

clojure-vagrant

a vagrant nginx-clojure testbuild
1
star
61

wp-disable-redis-object-cache-dropin

WordPress mu-plugin disabling Redis Object Cache for WordPress at various occasions
PHP
1
star
62

teamdaily

JavaScript
1
star
63

geniem-transit

Geniem Export Import Tool for Tempo
TypeScript
1
star
64

docker-wordpress-development

Development version of our WordPress docker image
1
star
65

wp-define-more

Adds useful definable constants which are missing from the WordPress Core
Shell
1
star
66

dustpress-comments

Comments Helper for DustPress - A WordPress plugin that adds a DustPress helper enabling ajaxified commenting for your website.
PHP
1
star
67

pipeline-aatt

Automated accessibility tests for Geniem Pipeline
JavaScript
1
star
68

geniem-rules-stylelint

Geniem Oy stylelint rules
JavaScript
1
star
69

wp-frontend-login

A plugin to log in into multiple environments at once.
PHP
1
star
70

RediSearch

ARM builds for RediSearch
Dockerfile
1
star
71

linkedevents-client

LinkedEvents Base Client
PHP
1
star
72

tms-theme-amuri

PHP
1
star
73

ga-popular-posts-geniem

WordPress library that provides a set of methods to fetch and use most popular posts from Google Analytics
PHP
1
star
74

tms-plugin-boilerplate

Tampere Multisite Plugin Boilerplate
PHP
1
star
75

wp-noindex-testing-staging-robots

Deny site from robots.txt if it's accessed in WP_ENV staging or testing or with testing domain
PHP
1
star
76

tms-plugin-manual-events

Manual events CPT and functionalities
PHP
1
star
77

wp-cookiebot-helper

WordPress plugin for displaying placeholder content with Cookiebot
PHP
1
star