PHPackages                             larawelp/options - 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. [Framework](/categories/framework)
4. /
5. larawelp/options

ActiveLibrary[Framework](/categories/framework)

larawelp/options
================

Package for creating options page in WordPress

v0.0.10(2y ago)13MITPHPPHP ^8.0

Since Aug 20Pushed 2y ago1 watchersCompare

[ Source](https://github.com/larawelp/options)[ Packagist](https://packagist.org/packages/larawelp/options)[ Docs](https://github.com/larawelp/options)[ RSS](/packages/larawelp-options/feed)WikiDiscussions 0.x Synced 1mo ago

READMEChangelogDependenciesVersions (11)Used By (0)

A simple framework for creating WordPress options page.

May be easily used in LaraWelP themes.

\#Basic Usage

Here is an example for creating an options page.

```
use LaraWelP\Options\OptionsPage;

$optionsPage = new OptionsPage([
    'menuSlug'    => 'my_options_page',
    'menuTitle'   => 'My Options Page',
    'pageTitle'   => 'My Options Page',
    'iconUrl'     => 'dashicons-welcome-learn-more',
    'optionGroup' => 'my_options_page',
    'optionName'  => 'my_options',
    'capability'  => 'manage_categories',
    'sections'    => [
        [
            'id'          => 'section-id',
            'title'       => 'Section title',
            'description' => 'Section Description',
            'fields'      => [
                [
                    'id'          => 'my-avatar',
                    'type'        => 'media',
                    'title'       => 'Avatar',
                    'description' => 'Choose an image for your avatar.'
                ],
                [
                    'id'    => 'my-email',
                    'type'  => 'email',
                    'title' => 'E-mail',
                ],
                [
                    'id'         => 'my-nice-name',
                    'type'       => 'text',
                    'title'      => 'Nice name',
                    'attributes' => [
                        'placeholder' => 'your nice name',
                        'maxlength'   => 10,
                        'class'       => 'regular-text'
                    ],
                ],
                [
                    'id'    => 'my-description',
                    'type'  => 'textarea',
                    'title' => 'About Me',
                ],
            ]
        ]
    ],
    'helpTabs'    => [
        [
            'title'    => 'tab-1',
            'content'  => 'description here',
        ],
        [
            'title'    => 'tab-2',
            'content'  => 'description here',
        ]
    ],
    'scripts'     => ['https://unpkg.com/vue/dist/vue.js'],
    'styles'      => ['/my-css.css'],
]);

$optionsPage->register();
```

The example code above is going to create an options page looks like this

[![](https://camo.githubusercontent.com/37a58e447a06f2b59a6373ef318ad8e06445e33c2953a78acfb40276f66ecf7a/68747470733a2f2f3164657369676e2e6a702f77702d636f6e74656e742f75706c6f6164732f323031372f30312f6f7074696f6e732d706167652e706e67)](https://camo.githubusercontent.com/37a58e447a06f2b59a6373ef318ad8e06445e33c2953a78acfb40276f66ecf7a/68747470733a2f2f3164657369676e2e6a702f77702d636f6e74656e742f75706c6f6164732f323031372f30312f6f7074696f6e732d706167652e706e67)

Usage of `OptionsSection` and `OptionsField`
--------------------------------------------

[](#usage-of-optionssection-and-optionsfield)

You can also replace the value of `'sections'` with an array of `OptionsSection` objects and replace the value of `'fields'` with an array of `OptionsField` objects.

```
use LaraWelP\Options\OptionsPage;

/*------------------------------------*\
    # Field objects
\*------------------------------------*/

$avatarField = new OptionsField([
    'id'          => 'my-avatar',
    'type'        => 'media',
    'title'       => 'Avatar',
    'description' => 'Choose an image for your avatar.'
]);

$emailField = new OptionsField([
    'id'    => 'my-email',
    'type'  => 'email',
    'title' => 'E-mail',
]);

$niceNameField = new OptionsField([
    'id'         => 'my-nice-name',
    'type'       => 'text',
    'title'      => 'Nice name',
    'attributes' => [
        'placeholder' => 'your nice name',
        'maxlength'   => 10,
        'class'       => 'regular-text'
    ]
]);

$descriptionField = new OptionsField([
    'id'    => 'my-description',
    'type'  => 'textarea',
    'title' => 'About Me',
]);

$demoField = new OptionsField([
    'id'    => 'my-demo',
    'type'  => 'text',
    'title' => 'Demo text field',
]);

/*------------------------------------*\
    # Section object
\*------------------------------------*/

$demoSection = new OptionsSection([
    'id'          => 'section-id',
    'title'       => 'Section title',
    'description' => 'Section Description',
    'fields'      => [
        $demoField,
    ]
]);

/*------------------------------------*\
    # Page object
\*------------------------------------*/

$optionsPage = new OptionsPage([
    'menuSlug'    => 'my_options_page',
    'menuTitle'   => 'My Options Page',
    'pageTitle'   => 'My Options Page',
    'iconUrl'     => 'dashicons-welcome-learn-more',
    'optionGroup' => 'my_options_page',
    'optionName'  => 'my_options',
    'capability'  => 'manage_categories',
    'sections'    => [
        [
            'id'          => 'section-id',
            'title'       => 'Section title',
            'description' => 'Section Description',
            'fields'      => [
                $avatarField,
                $emailField,
                $niceNameField,
                $descriptionField,
            ]
        ],

        $demoSection,
    ],
    'helpTabs'    => [
        [
            'title'   => 'tab-1',
            'content' => 'description here',
        ],
        [
            'title'   => 'tab-2',
            'content' => 'description here',
        ]
    ],
    'scripts'     => ['https://unpkg.com/vue/dist/vue.js'],
    'styles'      => ['/my-css.css'],
]);

/*------------------------------------*\
    # register page/section/field
\*------------------------------------*/

// register page
$optionsPage->register();

// register a section to a page(Settings -> General)
$demoSection->register('general', 'demo-section-group', 'demo-section-options');

// register a field to a section of page(Settings -> General -> `default` section)
$demoField->register('general', 'default', 'demo-section-group', 'demo-section-options');
```

Get the value of an option
--------------------------

[](#get-the-value-of-an-option)

You can use the `OptionsRepository` to get the value of an option.

```
use LaraWelP\Options\OptionsRepository;

// Get the value of 'my-nice-name' in 'my_options'.
// 'my_options' is the option name.
$myOptions = new OptionsRepository('my_options');
echo $myOptions->get('my-nice-name');

// also you can set the value by calling the set() method.
$myOptions->set('my_options','new value');
```

`OptionsPage`
=============

[](#optionspage)

Options
-------

[](#options)

### `menuSlug`

[](#menuslug)

TyperequiredstringyesThe slug name to refer to the menu by (should be unique for this menu).

### `menuTitle`

[](#menutitle)

TyperequiredstringyesThe text to be used for the menu.

### `pageTitle`

[](#pagetitle)

TyperequiredstringyesThe text to be displayed in the title tags of the page when the menu is selected.

### `optionGroup`

[](#optiongroup)

TyperequiredstringyesThe option group you wish to use in the page.

### `optionName`

[](#optionname)

TyperequiredstringyesThe option name you wish to use in the page.

### `capability`

[](#capability)

TypeDefaultstring'manage\_options'The [capability](https://codex.wordpress.org/Roles_and_Capabilities) required for this menu to be displayed to the user.

### `position`

[](#position)

TypeDefaultintnullThe position in the menu order this one should appear.

### `iconUrl`

[](#iconurl)

TypeDefaultstring''The URL (or [icon name](https://developer.wordpress.org/resource/dashicons/)) to the icon to be used for the menu.

### `parent`

[](#parent)

TypeDefaultnull / string / OptionsPageContractnullIf you wish to make the page as a sub-page of a top-level page, set the top top-level page here.

### `sections`

[](#sections)

TypeDefaultarray\[\]Settings-Sections to be inserted into the page. Every element of this array represents a Settings-Section.
See [Options for OptionsSection](#options-1) for more details.

### `renderFunction`

[](#renderfunction)

TypeDefaultcallablenullThe function to be called to output the content for the page.
This function retrieves two arguments; the first one is an instance of `OptionsPage`, the second one is an instance of `OptionsForm`. By using the `OptionsForm` object you can create form input elements much easier than by hard coding.

Below is an example of customizing the output of an options page.

```
use LaraWelP\Options\OptionsPage;

$optionsPage = new OptionsPage([
    'menuSlug'       => 'my_options_page',
    'menuTitle'      => 'My Options Page',
    'pageTitle'      => 'My Options Page',
    'optionGroup'    => 'my_options_page',
    'optionName'     => 'my_options',
    'renderFunction' => function (OptionsPage $page, OptionsForm $form) {
        ?>
