Home >Backend Development >PHP Tutorial >Typecho plug-in writing tutorial (3): Save configuration, typecho plug-in_PHP tutorial

Typecho plug-in writing tutorial (3): Save configuration, typecho plug-in_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:52:321040browse

Typecho plug-in writing tutorial (3): Save configuration, typecho plug-in

We made a bare plug-in in the previous section, now we start to make our plug-in work!

I. Improve the method

Two methods

We implement the activate and deactivate methods
Copy code The code is as follows:
Public static function activate(){
          return 'activate';
}

public static function deactivate(){
          return 'deactivated';
}

As shown in the above code, we have return values ​​in the activation and uninstallation plug-in methods, so there will be corresponding prompts during the corresponding operations.

Improve the information and make it more down-to-earth
Copy code The code is as follows:
Public static function activate(){
               // do something
             return 'The plug-in is installed successfully, please enter the settings to fill in the access key';
}

public static function deactivate(){
               // do something
           return 'Plug-in uninstalled successfully';
}

II. How to save configuration

Where are the access keys stored? Of course it's the database.

Typecho has implemented the Typecho_Widget_Helper_Form class for us. We only need a little code to get rid of the trouble of writing the form ourselves.

The following figure shows the inheritance relationship of the form class. We can use many types of forms to save our options.

Next we save the interface call address in the config method, similar to the following link (you can find it in the link submission of Baidu Webmaster Platform)

Interface calling address: http://data.zz.baidu.com/urls?site=www.phpgao.com&token=5wK0QtGCzdRzufvW
Copy code The code is as follows:
Public static function config(Typecho_Widget_Helper_Form $form){
//Save the interface calling address
           $element = new Typecho_Widget_Helper_Form_Element_Text('api', null, null, _t('Interface call address'), 'Please log in to Baidu Webmaster Platform to obtain');
$form->addInput($element);
}

There are 5 initialization parameters. What do they do?

The following is the construction method of the form base class. Their functions are form input name, selection item, form default value, form title, and form description.
Copy code The code is as follows:
# var/Typecho/Widget/Helper/Form/Element.php:111

/**
* Constructor
*
* @access public
* @param string $name form input name
* @param array $options options
* @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)
# Omit the following

III. Issues needing attention when using

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 is persisted to the database. Only by disabling the plug-in can the plug-in's form settings be cleared
Typecho_Widget_Helper_Form_Element_Fake Ignore
From var/Widget/Plugins/Edit.php we learned a lot of advanced usage of forms, which I will mention at the appropriate time in the future.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1008008.htmlTechArticleTypecho plug-in writing tutorial (3): Save configuration, typecho plug-in In the previous section, we made a bare plug-in, below Let’s get our plugin working! I. Improvement methods Two methods...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn