Typecho plugin compilation tutorial (3): save configuration

Source: Internet
Author: User
This article mainly introduces the typecho plug-in compiling tutorial (3): Saving the configuration. This article describes the complete method, how to save the configuration, and precautions for use, you can refer to the previous section to create a bare plug-in. let's start working on the plug-in!

I. improvement methods

Two methods

We implement the activate and deactivate methods.

The code is as follows:


Public static function activate (){
Return 'activate ';
}

Public static function deactivate (){
Return 'activated ';
}


As shown in the code above, we have returned values in the method of activating and uninstalling the plug-in, so there will be corresponding prompts during the corresponding operation.

Improve information to make it more grounded

The code is as follows:


Public static function activate (){
// Do something
Return 'plugin installed successfully. enter the settings and enter the accesskey ';
}

Public static function deactivate (){
// Do something
Return 'plugin uninstalled successfully ';
}

II. how to save the configuration

Where can I save the access key? Of course it is a database.

Typecho has implemented the Typecho_Widget_Helper_Form class for us. we only need a bit of code to get rid of the trouble of writing forms by ourselves.

Is the inheritance relationship of form classes. we can use many types of forms to save our options.

Below we save the interface call address in the config method, similar to the following link (you can find it in the link submission on the Baidu webmaster platform)

Interface call 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 the API call address
$ Element = new Typecho_Widget_Helper_Form_Element_Text ('api ', null, null, _ t ('interface call address'), 'Please log on to Baidu webmaster platform to obtain it ');
$ Form-> addInput ($ element );
}


There are five initialization parameters. what do they do?

The following are the construction methods of form base classes. Their functions are form input item name, selection item, form default value, form title, form description.

The code is as follows:


# Var/Typecho/Widget/Helper/Form/Element. php: 111

/**
* Constructor
*
* @ Access public
* @ Param string $ name of the input item in the form
* @ Param array $ options option
* @ Param mixed $ value form default value
* @ 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)
# Omitted below

III. Precautions

After modifying the form name ($ name), you need to restart the plug-in to work, because after the plug-in is enabled, the form content will be persisted to the database. to disable the plug-in, you can clear the form settings of the plug-in.
Typecho_Widget_Helper_Form_Element_Fake ignore
From var/Widget/Plugins/Edit. php, we have learned a lot about the advanced usage of forms, which will be mentioned later when appropriate.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.