Hello!
I've just run into some trouble trying to use Widget Class. Everything else is working fine.
I get a following fatal error whenever I reference the Carbon_Fields\Widget:
_Fatal error: Class 'Carbon_Fields\Widget' not found in..._
I'm running PHP 7.0.9 and installed Carbon Fields (v.2.0.4) via Composer. My code is straight from the docs:
use Carbon_Fields\Widget;
use Carbon_Fields\Field;
class ThemeWidgetExample extends Widget {
// Register widget function. Must have the same name as the class
function __construct() {
$this->setup('Theme Widget - Example', 'Displays a block with title/text', array(
Field::make('text', 'title', 'Title')->set_default_value('Hello World!'),
Field::make('textarea', 'content', 'Content')->set_default_value('Lorem Ipsum dolor sit amet')
));
}
// Called when rendering the widget in the front-end
function front_end($args, $instance) {
echo $args['before_title'] . $instance['title'] . $args['after_title'];
echo '<p>' . $instance['content'] . '</p>';
}
}
function load_widgets() {
register_widget('ThemeWidgetExample');
}
add_action('widgets_init', 'load_widgets');
Can it possibly be a bigger issue? I believe I didn't miss anything in that one.
Thanks!
Hi @adamromanowski ,
Is it possible that you are defining the widget before you include the composer autoload.php file?
Also, please execute composer dump-autoload to make sure that the required Carbon Fields autoloader code is included.
Hi @atanas-angelov-dev
Thanks for reply :) I've got it like this:
use Carbon_Fields\Container;
use Carbon_Fields\Field;
use Carbon_Fields\Widget;
add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
require_once( ABSPATH . '/vendor/autoload.php' );
\Carbon_Fields\Carbon_Fields::boot();
}
class ThemeWidgetExample extends Widget {
Also, later I attach registering to 'widget_init' hook.
composer dump-autoload doesn't seem to solve the issue.
I believe the cause is that the class definition is indeed used before the autoload.php is actually loaded (the class is declared in the file but the autoloader.php file is loaded in a hook that is executed later even if it appears before the class in the source file).
Try moving your widget class declaration to a separate file and require that file immediately after \Carbon_Fields\Carbon_Fields::boot();.
Solved!
Although, just moving it to another file hasn't change anything, it was wrapping the class declaration in another function and hooking it to widget_init that's done the job.
Thanks so much for your help @atanas-angelov-dev !
@adamromanowski I have the same problem but I don't understand how you solved... can you post your code?
Hey @Birbamax74
Sure thing :) The problem was quite simple.
The function where you will use register_widget() must be hooked to _widgets_init_
function example_register_widgets() {
require_once( 'some-widget.php' );
}
add_action( 'widgets_init', 'example_register_widgets' );
You can place it in your functions.php or wherever it will be loaded to the main scope.
Then in included file you can work with Carbon Fields widget magic:
use Carbon_Fields\Widget;
use Carbon_Fields\Field;
class ThemeWidgetExample extends Widget {
// Register widget function. Must have the same name as the class
function __construct() {
$this->setup( 'theme_widget_example', 'Theme Widget - Example', 'Displays a block with title/text', array(
Field::make( 'text', 'title', 'Title' )->set_default_value( 'Hello World!') ,
Field::make( 'textarea', 'content', 'Content' )->set_default_value( 'Lorem Ipsum dolor sit amet' )
) );
}
// Called when rendering the widget in the front-end
function front_end( $args, $instance ) {
echo $args['before_title'] . $instance['title'] . $args['after_title'];
echo '<p>' . $instance['content'] . '</p>';
}
}
register_widget( 'ThemeWidgetExample' );
Now it's even covered in Carbon Fields docs. Whole trick about the issue is registering during widgets_init hook. Let me know if that helps :)
@adamromanowski you are great!! now it works.
thanks a lot
Most helpful comment
Solved!
Although, just moving it to another file hasn't change anything, it was wrapping the class declaration in another function and hooking it to widget_init that's done the job.
Thanks so much for your help @atanas-angelov-dev !