Association Field errors, when using CF within a plugin
Version
- Carbon Fields: 3.0.2
- WordPress: 5.0.x
- PHP: 7.1
Problem Statement
When loading Carbon Fields via composer within a plugin setting, any use of the association field type produces a JS error which prevents the field from rendering on the page.
Container definition
<?php
/**
* Plugin Name: Accordion Test Plugin
* Description: Testing plugin which should load Carbon Fields via composer and create an association field.
* Author: Steve Ryan
* Author URI: https://engineering.asu.edu
*/
// Load Carbon Fields, Define theme options page.
add_action( 'after_setup_theme', 'asufse_crb_load' );
function asufse_crb_load() {
require_once( plugin_dir_path(__FILE__) . '/vendor/autoload.php' );
\Carbon_Fields\Carbon_Fields::boot();
}
use Carbon_Fields\Container;
use Carbon_Fields\Field;
add_action( 'carbon_fields_register_fields', 'asefse_add_plugin_options' );
function asefse_add_plugin_options() {
Container::make( 'post_meta', 'Association Field Check' )
->where( 'post_type', '=', 'page' )
->add_fields( array(
Field::make( 'association', 'crb_association' )
->set_types( array(
array(
'type' => 'post',
'post_type' => 'page',
),
) )
));
}
Steps to Reproduce the Problem
Install the plugin, activate and create a new page. The JS errors will be present within the console.
Comments
- The exact same field definition works when Carbon Fields is loaded as a composer dependency within the context of a theme.
- The field also fails when being added to a different container context. (Tried it with a theme options page initially. The page was created and additional fields could also be created.)
I can bundle my WHOLE plugin as needed to fully attempt to replicate the problem. The plugin was initially created via WP-CLI and the scaffold plugin command. The only other modification was to composer require carbon fields within that specific folder.
LOL. Do all the work to post an issue then solve your own problem. This was a conflict between the loaded version of CF within our theme and the loaded version within the plugin setting. No need to load CF twice. Works perfectly when I simply include the field definition within the plugin.