First I downloaded https://carbonfields.net/zip/latest/ and installed the plugin at WP backend. I also activated it as well.
For this test case I use the "Twenty Sixteen" template with a fresh WordPress installation without any other plugins installed and according to the documentation page of Carbon Fields I added the following piece of code to the top of my functions.php file:
<?php // PHP 7
use Carbon_Fields\Container;
use Carbon_Fields\Field;
add_action( 'carbon_fields_register_fields', 'crb_attach_theme_options' );
function crb_attach_theme_options() {
Container::make( 'theme_options', 'Theme Options' )
-> set_page_menu_position( 0 )
-> add_fields( array(
Field::make( 'text', 'crb_text')
) );
}
Everything looks fine so far because "Theme Options" appears in WP backend just as expected.
Now I try to retrieve the field value crb_text
as follows:
// this snippet starts exactly where the previous one ended
add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
// require_once( ABSPATH . '/vendor/autoload.php' ); original from website throws: "Failed opening required" so modified to:
require_once( ABSPATH . 'wp-content/plugins/carbon-fields/vendor/autoload.php' );
\Carbon_Fields\Carbon_Fields::boot();
var_dump( carbon_get_theme_option( 'crb_text' ) ); // -> string(0) ""
var_dump( carbon_get_theme_option( '_crb_text' ) ); // -> string(0) "" isn't actually the right way to do it but give it a try for testing purpose
var_dump( get_option( '_crb_text' ) ); // -> string(4) "test"
}
As you can see I'm able to retrieve the data by calling get_option( '_crb_text' )
which is the native WP way but the plugins function carbon_get_theme_option( 'crb_text' )
does not work. Actually this would be fine for "simple fields" but there are "complex fields" that have to be retrieved by the plugins own functions which is carbon_get_theme_option()
in this case.
I also did have a look at this question: use Carbon Fields in custom plugin class. But this question ends where mine starts.
Thank you in advance...
PS: I'm used to work with Carbon Fields 1.6 which works fine with quite a similar setup but want to upgrade to branch 2.
My enviroment again: define('WP_DEBUG', true);
, Carbon Fields 2.1.0, WordPress 4.8.2–de_DE (fresh installation without other plugins than Carbon Fields), Twenty Sixteen 1.3, PHP 7
This is a quote from a chat which I had with one of the plugin authors "Atanas Angelov":
So here is one confirmed way to bootstrap Carbon Fields:
Just to emphasize the core issue here... That was the reply to the snippet above:
Notes For Clarification & Relevancy
This is only relevant if you want to retrieve the data earlier than in your theme files due to the fact that the
carbon_fields_fields_registered
action hook has already fired when your theme files are loaded. So in your theme files it should be totally fine to just call:This is the case for all "carbon_get_*" functions such as
carbon_get_post_meta()
,carbon_get_term_meta()
,carbon_get_user_meta()
,carbon_get_comment_meta()
.Useful For
If you ever want to retrieve data earlier than in your theme files make shure that this happens on
carbon_fields_fields_registered
action hook or that the hook was fired already.This could be the case if you develop a WP plugin in which you integrate Carbon Fields (which is true for me). When you bootstrap your plugin the
carbon_fields_fields_registered
action hook didn't happen so make sure to have the right timing.Alternative
As also mentioned in the question you can also use:
when you want to retrieve data that is set by:
but this goes along with the following disadvantages:
Field::make(...)->set_default_value( $default_value )
(in contrast to Carbon Fields methods).