wp4laravel/corcel-acf
Adds Advanced Custom Fields (ACF) support to Corcel so you can access WordPress custom field values from Laravel via familiar Eloquent-style models and relationships. Integrates ACF fields into your Corcel queries and attributes.
Installation
composer require wp4laravel/corcel-acf
Ensure wp4laravel/corcel is also installed (Corcel is a Laravel package for WordPress).
Service Provider
Add to config/app.php under providers:
Wp4laravel\CorcelAcf\CorcelAcfServiceProvider::class,
Publish Config (Optional)
php artisan vendor:publish --provider="Wp4laravel\CorcelAcf\CorcelAcfServiceProvider"
Config file: config/corcel-acf.php.
First Use Case Fetch an ACF field from a WordPress post:
use Wp4laravel\Corcel\Post;
use Wp4laravel\CorcelAcf\Facades\Acf;
$post = Post::find(1);
$customField = Acf::getField('field_123abc', $post); // 'field_123abc' is your ACF field key
Fetching ACF Fields
$title = Acf::getField('field_title', $post);
$fields = Acf::getFields($post);
$flexibleContent = Acf::getField('field_flexible', $post);
// Process nested layouts/rows
Validation & Type Handling
$date = Acf::getField('field_date', $post)->castAsDateTime();
Integration with Eloquent
use Wp4laravel\CorcelAcf\HasAcf;
class CustomPost extends Post
{
use HasAcf;
protected $acfFields = ['field_title', 'field_content'];
}
Now access fields directly:
$post = CustomPost::find(1);
$post->field_title; // No need for Acf::getField()
Repeater Fields
$repeater = Acf::getField('field_repeater', $post);
foreach ($repeater as $row) {
$row['sub_field_key']; // Access sub-fields
}
Relationships
$relatedPosts = Acf::getField('field_related_posts', $post);
// $relatedPosts is a collection of Post models
Field Key vs. Name
field_123abc) instead of field names (e.g., title) for reliability. Field names can change in the WordPress UI, but keys are stable.Caching Quirks
php artisan corcel:clear-cache
config/corcel-acf.php):
'cache' => false,
Flexible Content Depth
Acf::getField() with the full layout key:
$layout = Acf::getField('field_layout_123', $post, 'layout_key');
Post vs. User Context
Post, User, or Term):
// Wrong (if $user has no ACF fields)
$user = User::find(1);
Acf::getField('field_title', $user); // May return null
WordPress Multisite
Acf::setBlogId(2); // Switch to blog ID 2
Check Field Existence
wp_json_and_bail() in wp-admin/admin-ajax.php):
Acf::hasField('field_123abc', $post); // Returns boolean
Log Raw ACF Data
dd(Acf::getRawField('field_123abc', $post));
Handle Missing Fields
$value = Acf::getField('field_maybe_missing', $post, 'default_value');
Custom Field Types
Acf::extend('my_custom_field_type', function ($value, $field) {
return processCustomValue($value);
});
Event Hooks
use Wp4laravel\CorcelAcf\Events\AcfFieldUpdated;
AcfFieldUpdated::listen(function ($event) {
// React to ACF field changes
});
Bulk Operations
Acf::syncFields() to update fields in bulk:
$posts = Post::where('status', 'publish')->get();
Acf::syncFields($posts, ['field_title', 'field_content']);
How can I help you explore Laravel packages today?