A package to render form fields in WordPress settings pages.

Overview

WP Settings Page Fields

A package to render form fields in WordPress settings pages.

You might need a settings page with a form and its fields and you have to render the fields every time you create a custom plugin.

Maybe, you already have some snippets that generates the form fields for you, otherwise this package is for you.

Installation

You can install the package via composer:

composer require roelmagdaleno/wp-settings-page-fields

Usage

Every supported form element extends the Roel\WP\Settings\Element class and must be instantiated with the same constructor:

<?php

$element = new Element( $id, $settings, $option_name );

These are the supported parameters:

  • $id: The form element id. Use the id() method to get the id.
  • $settings: The form element settings (Some form elements have specific settings).
  • $option_name: The database option name where the value is stored.

WordPress

To register and render form elements in your settings page with this package, you can use:

<?php

use Roel\WP\Settings\Elements\{
    Checkbox,
    Text,
};

$settings = array(
    new Checkbox( 'allow-user', array(
        'label'       => 'Allow user?',
        'description' => 'Allow user to do current action.',
    ), 'rmr_settings' ),
    new Text( 'api-key', array(
        'label'       => 'API Key',
        'description' => 'Insert the API Key.',
    ), 'rmr_settings' ),
);

foreach ( $settings as $setting ) {
    add_settings_field(
        $setting->id(),
        $setting->label(),
        array( $setting, 'print' ),
        'your-page'
    );
}

Remember to add the array( $setting, 'print' ) as a callback to render the setting field. The $setting variable is an Roel\WP\Settings\Element instance.

Render

This is how you can create a form element instance and render the HTML:

Using the render method

<?php

use Roel\WP\Settings\Elements\Text;

$settings = array(
    'label'       => 'API Key',
    'description' => 'Insert your API Key in this form field.',
);

$text = new Text( 'api-key', $settings, 'rmr_settings' );

echo $text->render();

Using the print method

<?php

use Roel\WP\Settings\Elements\Text;

$settings = array(
    'label'       => 'API Key',
    'description' => 'Insert your API Key in this form field.',
);

$text = new Text( 'api-key', $settings, 'rmr_settings' );

$text->print();

Using echo directly to the instantiated class

<?php

use Roel\WP\Settings\Elements\Text;

$settings = array(
    'label'       => 'API Key',
    'description' => 'Insert your API Key in this form field.',
);

$text = new Text( 'api-key', $settings, 'rmr_settings' );

echo $text;

Attributes

name attribute

Every form element includes a name attribute. This attribute will be used to assign the current element value to the $_POST variable, so you can manage that value like insert it into the database.

The name attribute value is generated by using the option_name and id. So, in the next example:

<?php

use Roel\WP\Settings\Elements\Text;

$settings = array(
    'label'       => 'API Key',
    'description' => 'Insert your API Key in this form field.',
);

$text = new Text( 'api-key', $settings, 'rmr_settings' );

echo $text->render();

The name attribute would be:

<input type="text" name="rmr_settings[api-key]" />

Then, in PHP, you would access the value of that input like this:

$_POST['rmr_settings'] = array(
    'api-key' => 'my-api-key',
);

HTML attributes

You can add HTML attributes to your form elements:

<?php

use Roel\WP\Settings\Elements\Text;

$settings = array(
    'label'       => 'API Key',
    'description' => 'Insert your API Key in this form field.',
    'attributes'  => array(
        'data-value'  => 'value',
        'data-custom' => 'custom',
        'oninput'     => 'myFunction(\'myValue'\)'
    ),
);

$text = new Text( 'api-key', $settings, 'rmr_settings' );

echo $text->render();

Group

Look at this example:

<?php

use Roel\WP\Settings\Elements\{
    Checkbox,
    Text,
};

$settings = array(
    new Checkbox( 'allow-user', array(
        'label'       => 'Allow user?',
        'description' => 'Allow user to do current action.',
    ), 'rmr_settings' ),
    new Checkbox( 'allow-admin', array(
        'label'       => 'Allow admin?',
        'description' => 'Allow admin to do current action.',
    ), 'rmr_settings' ),
    new Text( 'api-key', array(
        'label'       => 'API Key',
        'description' => 'Insert the API Key.',
    ), 'rmr_settings' ),
);

foreach ( $settings as $setting ) {
    add_settings_field(
        $setting->id(),
        $setting->label(),
        array( $setting, 'print' ),
        'your-page'
    );
}

You see how the rmr_settings option name is repetitive? Now imagine to add more than 10 settings in the same page. For this kind of cases, you can use the Roel\WP\Settings\Group class.

The instance accepts two parameters:

  • $elements (array): The elements to group.
  • $option_name (string): The option name for each element.

The passed option name will be set to every passed form element.

<?php

use Roel\WP\Settings\Group;
use Roel\WP\Settings\Elements\{
    Checkbox,
    Text,
};

$elements = array(
    new Checkbox( 'allow-user', array(
        'label'       => 'Allow user?',
        'description' => 'Allow user to do current action.',
    ) ),
    new Checkbox( 'allow-admin', array(
        'label'       => 'Allow admin?',
        'description' => 'Allow admin to do current action.',
    ) ),
    new Text( 'api-key', array(
        'label'       => 'API Key',
        'description' => 'Insert the API Key.',
    ) ),
);

$group = new Group( $elements, 'rmr_settings' );

foreach ( $group->elements() as $element ) {
    add_settings_field(
        $element->id(),
        $element->label(),
        array( $element, 'print' ),
        'your-page'
    );
}

After declaring the Group class with the required parameters, you can get the elements with elements() method, so you can loop and render them.

Settings

Check the available settings and more in the Wiki section.

Filters

Every form element has two filters to change the HTML output:

  • Change the HTML output for all registered elements.
  • Change the HTML output for a specific element.

Check the filters for all form elements and more in the Wiki section.

Form Elements

These are the supported form elements so far:

You might also like...
This package provides a wrapper for Google Lighthouse to audit the quality of web pages with Laravel.

laravel-google-lighthouse This package is based on octoper/lighthouse-php. This package provides a wrapper for Google Lighthouse to audit the quality

A PHP/Laravel package to fetch Notion Pages and convert it to HTML!

Generate HTML from Notion Page This package converts all the blocks in a Notion page into HTML using Notion's API. For more details on Notion API, ple

Adds a compact
Adds a compact "easy-sort" mode to Repeater and Repeater Matrix, making those fields easier to sort when there are a large number of items.

Repeater Easy Sort Adds a compact "easy-sort" mode to Repeater and Repeater Matrix, making those fields easier to sort when there are a large number o

Silverstripe-searchable - Adds to the default Silverstripe search by adding a custom results controller and allowing properly adding custom data objects and custom fields for searching

SilverStripe Searchable Module UPDATE - Full Text Search This module now uses Full Text Support for MySQL/MariaDB databases in version 3.* Adds more c

Miniset allows you to create compact sets of fields that either combine into a string of classes, or return a simple array of values

Miniset allows you to create compact sets of fields that either combine into a string of classes, or return a simple array of values. Miniset

Kirby plugin to visually show hidden characters in all kind of input fields and their previews.
Kirby plugin to visually show hidden characters in all kind of input fields and their previews.

Kirby Hidden Characters Kirby plugin to visually show hidden characters in all kind of input fields and their previews. This includes white spaces and

Replace, concat strings or change number fields permanently using Grid Options
Replace, concat strings or change number fields permanently using Grid Options

It's Pimcore Bundle to replace ,concat strings or change number fields permanently using Grid Options. It will save replaced strings directly in object.

Magento default mysql settings

magento-mysql Magento default mysql settings default/startup settings for mysql database. please read this before changing anything! GENERAL OPTIONS

Installable com_content/category overrides with configuration settings to create card layouts
Installable com_content/category overrides with configuration settings to create card layouts

Installable com_content/caregory overrides with configuration settings to create card layouts. Uses default bootstrap css only.

Releases(v0.1.1)
Owner
Roel Magdaleno
Computer Engineer. WordPress Developer. Using PHP, Laravel, JS & Vue.js. Currently working at @wpbullet
Roel Magdaleno
Run your WP site on github pages, php innovation award winner https://www.phpclasses.org/package/12091-PHP-Make-a-WordPress-site-run-on-GitHub-pages.html

Gitpress Run wordpress directly on github pages Gitpress won the innovation award for may 2021 Read more about this https://naveen17797.github.io/gitp

naveen 13 Nov 18, 2022
Coder Metabox for WordPress - Create Pages, Posts Custom Meta Fields options

Coder Metabox for WordPress Coder Metabox for WordPress - Create Pages, Posts Custom Meta Fields options. Step 1 call coder-metabox.php file in functi

Ashikur Rahman 3 Feb 19, 2022
Sync Wordpress Pages and Posts (even custom post types + fields) from static Markdown + YAML files

Sync Markdown Files to WordPress Posts and Pages Static site generators let you use a revision-controlled tree of markdown files to make a site, but d

null 26 Sep 26, 2022
Glz custom fields - Unlimited Custom Fields for Textpattern

Unlimited custom fields for Textpattern This plugin sits under the Extensions tab in the back-end and gives your custom fields new life. You can final

Gerhard Lazu 21 Dec 1, 2019
Simple user settings facade for Hyperf. Settings are stored as JSON in a single database column, so you can easily add it to an existing table.

hyperf-user-settings Simple user settings util for hyperf Settings are stored as JSON in a single database column, so you can easily add it to an exis

lysice 1 Oct 15, 2021
Advanced Custom Fields Contact Form Extension (Beta)

Advanced Custom Fields Contact Form Extension (Beta) This is an extension for Advanced Custom Fields 5 for Wordpress, which enables you to create a co

Christine Pham 61 Oct 6, 2022
A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

null 16 Nov 11, 2022
A package to render Heroicons in your PHP application.

PHP Heroicons A package to render Heroicons in your PHP application. Preview the icons at heroicons.com, developed by Steve Schoger and Adam Wathan. I

Roel Magdaleno 3 Aug 13, 2021
PDF API. JSON to PDF. PDF Template Management, Visual HTML Template Editor and API to render PDFS by json data

PDF Template Management, Visual HTML Template Editor and API to render PDFS by json data PDF ENGINE VERSION: development: This is a prerelease version

Ajous Solutions 2 Dec 30, 2022
💳 WordPress/WooCoommerce Brazilian Fields in Registry

Brazilian Fields in WordPress Registry A wordpress plugin that can be used for customize your website. Brazilian fields are added to the wordpress reg

Vinicius Blazius Goulart 7 Sep 20, 2022