PHPackages                             wp-grogu/acf-manager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. wp-grogu/acf-manager

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wp-grogu/acf-manager
====================

An Object-Oriented library used to create ACF field groups and gutemberg blocks, and retreive the data easily. Requires Wordpress, ACF.

v1.3.3(8mo ago)73.4k—6.7%1MITPHPPHP ^8.0

Since Apr 19Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/WP-Grogu/ACF-Manager)[ Packagist](https://packagist.org/packages/wp-grogu/acf-manager)[ RSS](/packages/wp-grogu-acf-manager/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (17)Used By (0)

Grogu ACF Manager
=================

[](#grogu-acf-manager)

[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Latest Version on Packagist](https://camo.githubusercontent.com/efe962669b533fb13b27695e3004511dd25759fe8a7270ecfab5dc27a0689ff5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f77702d67726f67752f6163662d6d616e616765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/wp-grogu/acf-manager)[![Total Downloads](https://camo.githubusercontent.com/a5436f75cc8e8ca1b93f083e64468509fa77ea4dca2c459bf63aabd2656ef9cd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f77702d67726f67752f6163662d6d616e616765722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/wp-grogu/acf-manager)

- [Introduction](#introduction)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
    - [Create a field group](#usage-create-field-group)
    - [Create a Gutemberg block](#usage-create-gutemberg-block)
    - [Working with flexible content](#usage-flexible-content)
    - [Retreiving your fields (FieldSet class)](#usage-retreiving-fields)
    - [Field casting](#usage-casting)
- [Contribute](#contribute)

Introduction
------------

[](#introduction)

This package brings an object-oriented approach to Wordpress Advanced Custom Fields (ACF) plugin. ACF manager will help you to create field groups, gutemberg blocks, options pages and flexible content directly in individual PHP classes, so you keep a clean and structured app folder. Using this package will make forget about the ACF back-office interface, allowing you to version control your ACF groups and make it a *breeze* to push your changes to multiple environments.

In addition to thoses (awesome) features, you will also receive back `FieldSet` objects instead of arrays when retreiving your fields from the database, which enables fields casting into Models, classes, or any other transformed data based on the field name (eg. a field named "image" may become an `App\Models\Attachment` object, with all the corresponding attributes and methods).

Behing the scene, acf-manager uses a custom fork of `wordplate/extended-acf` package, coming with an explicit documentation, so make sure to checkout [the official repositiory](https://github.com/wordplate/extended-acf) to discover how to define fields.

To make use of Eloquent Models (and corresponding builts-in transformers) in your app, if not already included in your framework, we highly recommand having a look at the [ObjectPress](https://gitlab.com/tgeorgel/object-press) library which brings some of the best Laravel features in any Wordpress installation.

This package comes as a standalone but is fully compatible with [Bedrock/Sage 10](https://roots.io/sage/) stack, with a native Wordpress theme (with an autoload logic setted up), and probably with many other frameworks out here.

Basic usage
-----------

[](#basic-usage)

At it's most basic usage, the plugin may be used this way to create a field group :

```
# Define field group
class Header extends \Grogu\Acf\Entities\FieldGroup
{
    public function fields(): array
    {
        return [
            Text::make('Titre', 'title')
                ->required(),
            Image::make('Image')
                ->previewSize('medium')
                ->returnFormat('id')
                ->required(),
        ];
    }

    public function location(): array
    {
        return [
            Location::where('post_type', 'page'),
        ];
    }
}

# Boot field group
add_action('acf/init', fn () => Header::make()->boot());
```

This package has a config file to manage your blocks registration without the need to use wordpress hooks :

```
return [

    /*
    |--------------------------------------------------------------------------
    | The registred ACF field groups & layouts
    |--------------------------------------------------------------------------
    |
    | Register here your ACF groups. Please note that only groups with
    | at least a location needs to be registred (eg: don't need to
    | register groups being cloned in other group or flexible)
    |
    */

    'groups' => [
        App\Acf\Header::class,
    ],

    [...]
];
```

It's also a breeze to create Flexible content sections :

```
use Grogu\Acf\Entities\FieldGroup;

class FreeSection extends FieldGroup
{
    public function fields(): array
    {
        return [
            FlexibleContent::make('Components')
                    ->buttonLabel('Add a new section')
                    ->thumbnails(true)
                    ->modal([
                        'enabled' => true,
                        'col'     => '4',
                    ])
                    ->layouts([
                        Blocks\Header::make()->toLayout(),
                        Blocks\Services::make()->toLayout(),
                        Blocks\Worldwide::make()->toLayout(),
                    ]),
        ];
    }

    [...]
}
```

Finally, when receiving back your fields, you may parse them into Field sets to benefit from casting and fluent interface allowing multiple accessors on the class :

```

// views/header.blade.php

    {{ $fields->title }}

```

The `FieldSet` class is completely fluent and all of those methods are valid to get values :

```
$fields = FieldSet::make([
    'sub' => [
        'field' => 'foo',
    ],
]);

$fields->sub->field
$fields->sub?->field
$fields['sub']['field']
$fields->get('sub.field')
```

Ready to get started ?

Requirements
------------

[](#requirements)

ACF manager has some requirements :

- PHP `>= 8.0`
- Wordpress
- ACF plugin enabled

Installation
------------

[](#installation)

1. Use composer to install the package in your theme directory :

```
composer require wp-grogu/acf-manager
```

2. Create an `Acf` namespace inside your autoloaded `app/` folder, which may look like this :

```
- theme
    - config
    - app
        - Acf
            - Groups
            - Blocks
            - Templates
            - Options
        [...]
    [...]

```

4. Bootload the module in your `functions.php` file :

```
[...]
/*
|--------------------------------------------------------------------------
| Load Grogu ACF Manager
|--------------------------------------------------------------------------
|
| Grogu ACF Manager module helps us create and retreive ACF field groups.
| This line starts the engine, loads the config/acf.php config file,
| and register our field groups, gutemberg blocks and layouts.
|
*/

new Grogu\Acf\Core\Bootloader;
```

5. Create the `config/acf.php` file which will hold your configuration using WP-CLI :

```
wp grogu-acf install:config
```

Or copy/paste it directly from the [source file](https://github.com/WP-Grogu/ACF-Manager/blob/main/config/acf.php).

Usage
-----

[](#usage)

Every field group or gutemberg block has it's own individual class/file. Each of them has, obviously, some fields, and some may also have one or more locations. All of them are defined using the great [wordplate/extended-acf](https://github.com/wordplate/extended-acf) package, which provides a fluent API around ACF.

### Create a field group

[](#create-a-field-group)

To create a group, run the following WP-CLI command :

```
wp grogu-acf make:group GroupName
wp grogu-acf make:group GroupName --in:Templates # specify subdir
```

A new file is created :

```
