This article mainly introduces the Typecho plug-in writing tutorial (iii): Save the configuration, this article explains how to improve the method, how to save the configuration, the use of the need to pay attention to the content, you need friends can refer to the
In the previous section we have made a bare plugin, below we start to let our plugin begin to work!
I. Perfecting Methods
Two methods
We implement activate and Deactivate methods
The code is as follows:
public static function activate () {
return ' activate ';
}
public static function deactivate () {
return ' deactivated ';
}
As shown in the code above, we have the return value in the activation and unload plug-in method, so there is a corresponding prompt when the action is taken.
Perfect information to make it more grounded gas
The code is as follows:
public static function activate () {
Do something
Return ' plugin installed successfully, please enter the set Fill entry key ';
}
public static function deactivate () {
Do something
Return ' plugin uninstall succeeded ';
}
Ii. How to save the configuration
Where is the access key saved? The database, of course.
Typecho has already implemented the Typecho_widget_helper_form class for us, we just need a little bit of code to get rid of the trouble of having to write our own forms.
The following figure is the inheritance of a form class, and we can use many types of table forms to save our options.
Below we in the Config method to save the interface call address, similar to the following link (you can in Baidu Webmaster Platform link submitted to find)
Interface Invocation Address: Http://data.zz.baidu.com/urls?site=www.phpgao.com&token=5wK0QtGCzdRzufvW
The code is as follows:
public static function config (Typecho_widget_helper_form $form) {
Save Interface Call Address
$element = new Typecho_widget_helper_form_element_text (' API ', NULL, NULL, _t (' Interface call address '), ' please login to Baidu Webmaster platform to get ');
$form->addinput ($element);
}
There are 5 initialization parameters, what do they do?
The following are the construction methods for the table single classes, which are the form entry name, the selection, the form default, the form title, and the form description.
The code is as follows:
# var/typecho/widget/helper/form/element.php:111
/**
* Constructor
*
* @access Public
* @param string $name form entry name
* @param array $options selection
* @param mixed $value form default values
* @param string $label form title
* @param string $description form Description
* @return void
*/
Public function __construct ($name = null, array $options = NULL, $value = NULL, $label = NULL, $description = NULL)
# The following omitted
Iii. use of issues to be noted
After you modify the form name ($name), you need to restart the plug-in to work because the form content is persisted to the database after the plug-in is enabled, disabling the plug-in to empty the plug-in's form settings
Typecho_widget_helper_form_element_fake don't bother.
We learned a lot of advanced usage of forms from var/widget/plugins/edit.php, and later on, the old Gao would mention it at the right time.