use Carbon Fields in custom plugin class

2019-05-24 15:33发布

问题:

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?

回答1:

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();


回答2:

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();