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?
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
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 thecarbon_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: