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!
Preparation
Have you ever 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 the revision?
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 plug-in need to implement?
1. Real-time push of articles
2. Push historical data
3.Sitemap
Where is the interface calling address (API)?
Baidu webmaster backend, PHP interface example:
Copy code 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;
Get started
Let Lao Gao pirate the code of the HELLO_WORLD plug-in in the previous section, delete all comments, and add his own information.
Copy code 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. Lao Gao will prepare a naked plug-in every time he writes a plug-in.
We put it in usr/plugins/BaiduSubmitTest/Plugin.php
Go to the backend plug-in immediately, as shown in the picture
Why plug and play?
Because we have no way to implement the plug-in, it cannot be enabled.
In the next section we will make our plug-in fuller!
End of this section.