-
Notifications
You must be signed in to change notification settings - Fork 2
From readme
- Data Components (Options, Post Meta, Settings, Term Meta, Transients)
- Pages (WordPress Admin Pages)
- Plugins
- Themes
- Utils
Symfony components such as Validator, Forms, Dependency Injection, Twig (as part of Forms rendered engine).
Option is a component from the DataComponents
series. Option represents the native WordPress options associated with the get_option()
or update_option()
WordPress functions, which are not very convienent.
The Option component in WP Kit is options on steroids.
-
Each time you use the WordPress option, you have to manually type its name (unless you're using auto complete), and if your product has many options, you can get confused. WP Kit solves this issue.
-
Options in WordPress do not handle variable types. For example, if you save an
integer
variable, after saving and retrieving the value back, you receive astring
value, which you then need to manually convert back tointeger
. WP Kit solves this with DataTransformers so you always receive the data in your desired type. -
Also, now you can validate your options data. No more manual validations for emails, urls, strings... You can easily define the rules of validation (Constraints) and error messages.
-
And those are only the key features of the Option component! There are more.
Here is a simple sample usage of the WP Kit Option
class. It's very helpful to avoid conflicting options, for example from other plugins or WordPress options.
use Korobochkin\WPKit\Options\Option;
// Create the option instance
$option = new Option();
$option
// Set the name
->setName(Plugin::NAME . '_option_name')
// Set the value
->setValue('the value of option')
// Actually save the value in DB.
->flush();
A more advanced usage is creating unique classes for each option in your product, after which to use your option you just use this class to retrieve the value.
namespace Korobochkin\MyProject\Options;
use Korobochkin\WPKit\Options\Option;
class TokenOption extends Option {
public function __construct() {
$this->setName(Plugin::NAME . '_token');
}
}
// Anywhere in your code
// Create the option instance
$option = new TokenOption();
// Retrieve the value from DB.
$option->get();
Transients are very similar to Options. The only difference is using expiration
instead of autoload
.