Skip to main content

Custom Widget

Custom widgets should have there own file, recommended: inc/widgets/widget-name.php.

To make a new widget, just extend the CustomWidget class and add it to the register_custom_widgets method in inc/fx-widgets.php. You will then just need to add basic static values, setup in inputs and build the render method.

Example

class YourWidget extends CustomWidget {

static $widget_id = 'widget_title_widget';
static $options = array(
'title' => 'Widget Title',
'description' => 'Widget description.'
);
static $fields = array(
'title' => array(
'type' => 'textfield',
'title' => 'Title:',
'description' => ''
),
);

public function widget( $args, $instance ) {

$title = $instance['title'];

echo $args['before_widget'];

// Widget body here

echo $args['after_widget'];
}
}

Declare fields like you would for any custom post meta.

The widget function is like the render method. It is what is shown on the frontend. You can access input values via the instance array.

After making your widget, you'll need to add it to the widgets_init function. Just go to inc/fx-widgets.php and register the widget. Eg.

Require the source file first:

require_once('widgets/widget-your-widget.php');

Then add it to the register_custom_widgets function:

function register_custom_widgets() {
register_widget('AnotherWidget');
register_widget('YourWidget');
}