PHPackages                             moxie-lean/wp-widgets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. moxie-lean/wp-widgets

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

moxie-lean/wp-widgets
=====================

Control the WP widgets whilst using WP in as a headless CMS.

1.0.4(4y ago)033.5k↓50%[3 issues](https://github.com/wearenolte/wp-widgets/issues)2MITPHPPHP &gt;=7.4 || 8.0

Since Mar 22Pushed 4y ago2 watchersCompare

[ Source](https://github.com/wearenolte/wp-widgets)[ Packagist](https://packagist.org/packages/moxie-lean/wp-widgets)[ Docs](https://github.com/moxie-lean/wp-widgets)[ RSS](/packages/moxie-lean-wp-widgets/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (17)Used By (2)

WP Widgets
==========

[](#wp-widgets)

> Control the WP widgets whilst using WP in as a headless CMS. Now widgets will output JSON data via an API rather than

Getting Started
---------------

[](#getting-started)

The easiest way to install this package is by using composer from your terminal:

```
composer require moxie-lean/wp-widgets
```

Or by adding the following lines on your `composer.json` file

```
"require": {
  "moxie-lean/wp-widgets": "dev-master"
}
```

This will download the files from the [packagist site](https://packagist.org/packages/moxie-lean/wp-widgets)and set you up with the latest version located on master branch of the repository.

After that you can include the `autoload.php` file in order to be able to autoload the class during the object creation.

```
include '/vendor/autoload.php';
```

Usage
-----

[](#usage)

You first need to register the widgets you want to use using the `Register::init()` function. This function takes the following parameter:

```
[
    'lean' => [],
    'custom' => [],
]
```

Where 'lean' is the list of pre-defined widgets you want to use, and 'custom' is a list of any custom widgets you want to add for this project. For example:

```
\Lean\Widgets\Register::init([
    'lean' => [
        'LeanPreview',
        'LeanMenu,
    ],
    'custom' => [
        'MyProject\Widgets\MyCustomWidget'
    ],
]);
```

Note that Lean widgets can be registered with their short class name, whereas custom widgets need a fully qualified namespace.

All custom widget objects must extend the `\Lean\Widgets\Models\AbstractWidget` class. See below.

You can register widget areas using the usual WordPress function:

```
register_sidebar(
    [
        'id' => 'my-sidebar',
        'name' => 'Name',
        'description' => 'My new sidebar',
    ]
);
```

### Creating Custom Widgets

[](#creating-custom-widgets)

All custom widgets must extend the `\Lean\Widgets\Models\AbstractWidget` class. The simplest widget you can create just needs to implement the `__construct()` function, eg:

```
use Lean\Widgets\Models\AbstractWidget;

class MyWidget extends AbstractWidget {
	public function __construct() {
		parent::__construct( 'My Widget', 'Displays something really cool.' );
	}
}
```

By default it will use the widget's class name as the widget slug, converting it into dash format (e.g. MyWidget becomes my-widget). If you want to override the slug you can pass it as a third argument into the `parent::__construct()` function.

In addition there are a couple of other functions which you may want to use:

#### Post Registration

[](#post-registration)

The post registration function runs just after the widget is registered. You can use this to register ACF fields, e.g.:

```
public static function post_registration() {
    if ( function_exists( 'acf_add_local_field_group' ) ) :
    endif;
}
```

#### Get Data

[](#get-data)

This function returns the widget's data for use in an API. By default it will return the widget's title and all ACF fields. You can override it like this:

```
public function get_data() {
    $data = parent::get_data();

    return array_merge( ['more_data' => 'something'], $data );
}
```

#### Widget

[](#widget)

By default the widget will output an error message if you try to use it in a normal WordPress theme. If, however, you want to allow it to be used as a normal widget too, you can override the `widget`function:

```
public function widget( $args, $instance ) {
    ?>
