Читать книгу Professional WordPress Plugin Development - Brad Williams - Страница 90

Registering New Settings

Оглавление

The next step is to register your new settings. The function you need here is register_setting() and three parameters, used as follows:

<?php register_setting( option group, option name, args ); ?>

The register_setting()function accepts the following parameters:

 option_group: Group name for your settings.

 option_name: Name of an option to sanitize and save.

 args: Data used to describe the setting when registered.type: Type of data associated with this setting. Valid values are string, boolean, integer, and number.description: Description of the data for this setting.sanitize_callback: Callback function to sanitize the option's value.show_in_rest: Whether the data with this setting should be included in the REST API.default: Default value when calling get_option().

Now let's register your plugin settings using the register_setting() function. The first parameter you'll set is the setting group name, and the second parameter is the option name as you would use it in a get_option() call. The group name can be anything actually, but it's just simpler to name it the same as the option that will get stored in the database.

The third parameter is an array of additional arguments defining the type of data. In this case, it's a string and references your callback function, here named pdev_plugin_validate_options(), that will be passed all the settings saved in your form. You'll define this function later.

<?php $args = array( 'type' => 'string', 'sanitize_callback' => 'pdev_plugin_validate_options', 'default' => NULL ); register_setting( 'pdev_plugin_options', 'pdev_plugin_options', $args ); ?>

Professional WordPress Plugin Development

Подняться наверх