3

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.

screenshot Carbon Fields works in WP backend

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

Axel
  • 3,331
  • 11
  • 35
  • 58
  • hmmm, I don't understand why you try to get value in crb_load action? – Mykyta Dudariev Oct 05 '17 at 07:36
  • @NikitaDudarev because this is the recommended way according to the website. **Can you share the way you do it?** Would be awesome! Anyway I'm glad you actually came around - thank you... – Axel Oct 05 '17 at 10:29
  • `carbon_get_theme_option` return the value what you save in option. For example `` . And I know this kind of example start carbon_fields [link](http://joxi.ru/MAjdPdKtvo8j1m). – Mykyta Dudariev Oct 05 '17 at 10:36
  • So how do YOU do it? – Axel Oct 05 '17 at 11:01
  • What exactly? I don't understand – Mykyta Dudariev Oct 05 '17 at 11:46
  • Hey @NikitaDudarev, its me again :) Actually I had a chat with one of the plugins authors so: everything is fine now. Updated my answer accordingly... – Axel Oct 05 '17 at 13:00
  • Could be so, but usually this function is used to output information in the template. – Mykyta Dudariev Oct 05 '17 at 13:04
  • @NikitaDudarev yes - "**usually** this function is used to output information in the template". But **in general** this function (`carbon_get_theme_option()` and all other `carbon_get_*` functions) is used to get the data of your Carbon Fields. If you retrieve the data in template files you won't touch this issue but in case you want to retrieve the data earlier you have to respect some circumstances. See my updated answer or the answer I replied to your own question if you want to dig deeper... – Axel Oct 05 '17 at 19:46
  • oh thank you, I don't know this fact – Mykyta Dudariev Oct 05 '17 at 20:14

1 Answers1

5

This is a quote from a chat which I had with one of the plugin authors "Atanas Angelov":

Hi @Elstermann you couldn't get the value because in order to get a field's value it has to be defined first. All fields are defined in the carbon_fields_fields_registered hook so any carbonget* calls before that hook has fired will not work (since no fields are defined yet).

So here is one confirmed way to bootstrap Carbon Fields:

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' ) -> add_fields( array(
        Field::make( 'text', 'crb_text')
    ) );
}

add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
    require_once( ABSPATH . 'wp-content/plugins/carbon-fields/vendor/autoload.php' );
    \Carbon_Fields\Carbon_Fields::boot();
}

add_action( 'carbon_fields_fields_registered', 'crb_values_are_avail' );
function crb_values_are_avail() {
    var_dump( carbon_get_theme_option( 'crb_text' ) ); // -> string(0) "test"
}

Just to emphasize the core issue here... That was the reply to the snippet above:

Yes - carbon_fields_fields_registered should be the earliest you can get a field's value


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:

carbon_get_theme_option( 'your_name_of_a_carbon_field' );
// for example in the "header.php" in your theme directory you could use
<style>body{background-color:<?php
    echo carbon_get_theme_option( 'custom_body_background' );
?>}</style> // just to give a real life like example

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:

get_option( '_your_field_name_prepended_by_lodash' )

when you want to retrieve data that is set by:

Container::make( 'theme_options', 'Theme Options' ) -> add_fields()

but this goes along with the following disadvantages:

  1. this doesn't work with Complex Fields and
  2. you don't have access to the values set by Field::make(...)->set_default_value( $default_value ) (in contrast to Carbon Fields methods).
Community
  • 1
  • 1
Axel
  • 3,331
  • 11
  • 35
  • 58