2

I have a plugin which has no functionality so far. This is the current structure:

<?php
class Test
{
    public function __construct()
    {

    }
}

$wpTest = new Test();

I want to use the Carbon Fields WordPress plugin. After installing it I changed the structure according to the instructions from the website, only with the adaptation to OOP.

<?php
use Carbon_Fields\Container;
use Carbon_Fields\Field;

class Test
{

    public function __construct()
    {
        add_action( 'carbon_fields_register_fields', array( $this, 'crb_attach_theme_options') );
        add_action( 'after_setup_theme', array( $this , 'crb_load' ) );
    }

    public function crb_load()
    {
        require_once( 'vendor/autoload.php' );
        \Carbon_Fields\Carbon_Fields::boot();
    }

    public function crb_attach_theme_options()
    {
        Container::make( 'theme_options', __( 'Plugin Options', 'crb' ) )
            ->add_fields( array(
                Field::make( 'text', 'crb_text', 'Text Field' ),
            ) );
    }

}

$wpTest = new Test();

It does not work. How do I fix it?

Axel
  • 3,331
  • 11
  • 35
  • 58
Mykyta Dudariev
  • 462
  • 5
  • 17
  • Please explain what your expected result is and what you mean by "it does not work". Is it not doing what you expected? Are you getting an error? It might help to review [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) so you give us the information we need to help you. – FluffyKitten Aug 22 '17 at 19:12
  • @FluffyKitten The end result should be a custom field in the admin panel of wordpress. There are no errors or notifications. Therefore, I asked a slightly ambiguous question, asking for help. – Mykyta Dudariev Aug 22 '17 at 19:39
  • Please enable `WP_DEBUG` in your wp-config.php and check for errors again. Are you positive that this code is included in functions.php? Also, are you sure that `vendor/autoload.php` is included? – Emil M Aug 23 '17 at 13:14
  • @EmilM I already solved this problem, now I am writing the answer. Thank you for paying attention to my question. – Mykyta Dudariev Aug 23 '17 at 13:36
  • Dear @NikitaDudarev maybe you could help me out with [my question](https://stackoverflow.com/questions/46575565/how-to-make-carbon-fields-2-1-0-work-carbon-get-theme-option) I'm not able to retrieve data with `carbon_get_theme_option()`. Would be awesome if you could have a look :) – Axel Oct 04 '17 at 23:20

2 Answers2

4

The answer from the questions author itself may work for his very own specific purpose.

But if you come a long this question chances are that you want to intergrate Carbon Fields in your own plugin (due to the verbalization of this question). In this case there is (at least) one issue you should be aware of, namely the point at which the data of your Carbon Fields is available; in case you want to retrieve Carbon Fields data at the time where the execution of your plugin happens.

TL;DR: In carbon_fields_fields_registered action hook is the earliest phase in which you can get a Carbon Fields value. These fields first have to be defined in the carbon_fields_register_fields action hook. For additional explanations you can also have a look at this answer.

So here is a bootstrap that makes sure to have a proper timing:

use Carbon_Fields\Container;
use Carbon_Fields\Field;

class YourFancyPlugin
{

    public function __construct()
    {
        add_action( 'after_setup_theme', array( $this,
            'load_carbon_fields'
        ) );

        add_action( 'carbon_fields_register_fields', array( $this,
            'register_carbon_fields'
        ) );

        /* will succesfuly retrieve the data of the fields registered at
         * carbon_fields_register_fields action hook
         * if you retrieve the data before carbon_fields_fields_registered action hook
         * has fired it won't work
         */
        add_action( 'carbon_fields_fields_registered', array( $this,
            // picked this name only to emphasize whats going on
            'carbon_fields_values_are_available'
        ) );

        /* do all the stuff that doesn't rely on values of your Carbon Fields */
    }

    public function load_carbon_fields()
    {
        require_once 'vendor/autoload.php'; // modify depending on your actual setup
        \Carbon_Fields\Carbon_Fields::boot();
    }

    public function register_carbon_fields()
    {
        Container::make( 'theme_options', 'YourFancyPlugin options' )
            -> add_fields( array(
                Field::make( 'text', 'YourFancyPlugin_option_1')
            ) );
    }

    public function carbon_fields_values_are_available()
    {
        /* retrieve the values of your Carbon Fields related to your plugin */
        var_dump( carbon_get_theme_option( 'YourFancyPlugin_option_1' ) );
        /* do all the stuff that does rely on values of your Carbon Fields */
    }

}

new YourFancyPlugin();
Axel
  • 3,331
  • 11
  • 35
  • 58
3

I found the answer to my question. From the part, the problem was that I connected the vendor/autoload.php after accessing the __construct().

An example of solving this task below

use Carbon_Fields\Container;
use Carbon_Fields\Field;



class PluginOption
{

    public function __construct()
    {
        require_once( 'vendor/autoload.php' );
        \Carbon_Fields\Carbon_Fields::boot();
        add_action( 'carbon_fields_register_fields', array( $this, 'crb_attach_theme_options') );
    }

    public function crb_attach_theme_options()
    {
        Container::make( 'theme_options', __( 'Plugin Option', 'crb' ) )
        ->add_fields( array(
            Field::make( 'text', 'crb_text', 'Text Field' ),
            ) );
    }

}

$wpTest = new PluginOption();
Mykyta Dudariev
  • 462
  • 5
  • 17