Читать книгу Professional WordPress Plugin Development - Brad Williams - Страница 77
Saving Options
ОглавлениеYou start by saving your first plugin option, which will be named pdev_plugin_color
and have a value of red. The function call to do so is the following:
<?php add_option( 'pdev_plugin_color', 'red' ); ?>
The add_option()
function accepts the following parameters:
option: Name of the option to add
value: Value of the option you are adding
deprecated: Description, which is no longer used
autoload: Whether to load the option when WordPress starts
The first parameter is your option name. It is crucial that you make it unique and self‐explanatory.
Unique: It will never conflict with internal existing or future WordPress options or with settings that might be created by another plugin.
Self‐explanatory: Name it so that it's obvious it's a plugin setting and not something created by WordPress.
NOTE Using the same prefix, for example, pdev_plugin
, for function names, options, and variables is highly recommended for code consistency and for preventing conflict with other plugins. The golden rule of “Prefix everything,” first introduced in Chapter 2, applies here.
The second parameter is the option value that can be practically anything a variable can hold: string, integer, float number, Boolean, object, or array.