Using options
The functions save_option and get_option are used to store or retrieve simple key-value data. Each entry is identified by a name and a group it belongs to, which are passed as arguments.
Getting options
You can add custom options for every module. For example now we will add dynamic text, which can be changed from the module "settings" part.
Example userfiles/modules/example_module/index.php
The $params['id']
value represents the current module instance ID. If you wish to get options from another group you can use your own value as the second parameter.
Saving options automatically
Just add mw_option_field
class to any input field.
Example userfiles/modules/example_module/admin.php
In admin.php you can have an input field with the class mw_option_field
. When you change this field, its value will be saved in the database automatically.
The name
attribute of the input is used as a key in the get_option
function, which you can use to retrieve the value from PHP. The value is saved for the current module instance. If you wish to store option for another instance you can set a custom option-group
attribute on your input field.
Saving options manually
You can save options also by AJAX by using the REST api and calling the save_option
function.
For example, now we will save a field with name text_color
Example userfiles/modules/example_module/admin.php
The options are simple way for making your module have dynamic content.
Last updated