This article mainly introduces the typecho plug-in writing tutorial (2): Write a new plug-in. This article is a series of articles The second article, friends in need can refer to it
In the first section, we learned about the basic structure of a plug-in. Next, we need an example to practice and consolidate.
What a coincidence, Lao Gao is currently revising the Baidu sitemap submission plug-in for typecho. Let’s revise it with Lao Gao!
Prepare
I wonder if you have used the WP version of Baidu structured plug-in? Lao Gao studied that plug-in, observed its API, and then wrote the typecho version.
Why do we need to revise the version?
Baidu webmaster recently launched a new interface, which is easier to use and the workload is not heavy, so just change it!
What functions does the new version of the plug-in need to implement?
1. Real-time push of articles
2. Push historical data
3.Site map
Where is the interface calling address (API)?
Baidu webmaster backend, PHP interface example:
The code is as follows:
$urls = array(
'http://www.example.com/1.html',
'http://www.example.com/2.html',
);
$api = 'http://data.zz.baidu.com/urls?site=www.phpgao.com&token=your access key';
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
echo $result;
Start taking action
Let Lao Gao first pirate the code of the HELLO_WORLD plug-in in the previous section, delete all comments, and add his own information.
The code is as follows:
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Baidu structured plug-in tutorial version
*
* @package BaiduSubmitTest
* @author 老高
* @version 0.4
* @link http://www.phpgao.com/typecho_plugin_baidusubmit.html
*/
class BaiduSubmitTest_Plugin implements Typecho_Plugin_Interface
{
public static function activate(){}
Public static function deactivate(){}
Public static function config(Typecho_Widget_Helper_Form $form){}
Public static function personalConfig(Typecho_Widget_Helper_Form $form){}
public static function render(){}
}
Lao Gao named the above code a naked plug-in, which means a plug-in that can’t do anything. Every time he writes a plug-in, Lao Gao will prepare a naked plug-in.
We will put it in usr/plugins/BaiduSubmitTest/Plugin.php
Immediately go to the backend plug-in, as shown in the picture
Why plug and play?
Since we don’t have a way to implement the plug-in, we can’t enable it.
In the next section we will make our plug-in richer!
This section is over.