Download the PHP package roelmagdaleno/wp-settings-page-fields without Composer
On this page you can find all versions of the php package roelmagdaleno/wp-settings-page-fields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download roelmagdaleno/wp-settings-page-fields
More information about roelmagdaleno/wp-settings-page-fields
Files in roelmagdaleno/wp-settings-page-fields
Package wp-settings-page-fields
Short Description A package to render form fields in WordPress settings pages.
License MIT
Informations about the package wp-settings-page-fields
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:
Usage
Every supported form element extends the Roel\WP\Settings\Element
class and must be instantiated with the same constructor:
These are the supported parameters:
$id
: The form element id. Use theid()
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:
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
Using the print
method
Using echo
directly to the instantiated class
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:
The name
attribute would be:
Then, in PHP, you would access the value of that input like this:
HTML attributes
You can add HTML attributes to your form elements:
Group
Look at this example:
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.
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: