Get the App
SLTechnology News&Howtos  ›  Development  › 

Introduction to how to write core code for typecho plug-in

Shulou Source: shulou.com Published: 2022-06-03 17:33:10 09月12日 Update

This article will explain in detail how to write the core code for the typecho plug-in. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

To analyze, when publishing an article, the information we need is the URL of the current article, and we need to find a way to get it from $contents and $class.

At present, our plug-in class code is as follows (please note that render has been changed to send)

The copy code is as follows:

Class BaiduSubmitTest_Plugin implements Typecho_Plugin_Interface

{

Public static function activate () {

/ / Mount the API for publishing articles and pages

Typecho_Plugin::factory ('Widget_Contents_Post_Edit')-> finishPublish = array (' BaiduSubmitTest_Plugin', 'send')

Typecho_Plugin::factory ('Widget_Contents_Page_Edit')-> finishPublish = array (' BaiduSubmitTest_Plugin', 'send')

Return 'plug-in installed successfully, enter the settings and fill in the admission key'

}

Public static function deactivate () {

/ / do something

Return 'plug-in uninstalled successfully'

}

Public static function config (Typecho_Widget_Helper_Form $form) {

$element = new Typecho_Widget_Helper_Form_Element_Text ('api', null, null, _ t (' access key'), 'Please log in to Baidu webmaster platform to get it')

$form- > addInput ($element)

}

Public static function personalConfig (Typecho_Widget_Helper_Form $form) {}

Public static function send ($contents, $class) {

/ / do something

}

}

Get URL

Obtaining permanent links needs to be jointly generated by routing rules and Typecho_Common::url!

The copy code is as follows:

Class BaiduSubmitTest_Plugin implements Typecho_Plugin_Interface

{

Public static function activate () {

/ / Mount the API for publishing articles and pages

Typecho_Plugin::factory ('Widget_Contents_Post_Edit')-> finishPublish = array (' BaiduSubmitTest_Plugin', 'send')

Typecho_Plugin::factory ('Widget_Contents_Page_Edit')-> finishPublish = array (' BaiduSubmitTest_Plugin', 'send')

Return 'plug-in installed successfully, enter the settings and fill in the admission key'

}

Public static function deactivate () {

/ / do something

Return 'plug-in uninstalled successfully'

}

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 (' API call address'), 'Please log in to Baidu webmaster platform to get it')

$form- > addInput ($element)

}

Public static function personalConfig (Typecho_Widget_Helper_Form $form) {}

/ * *

* prepare data

* @ param $contents article content

* @ param $class class that calls the interface

* @ throws Typecho_Plugin_Exception

, /

Public static function send ($contents, $class) {

/ / if the article attribute is hidden or delayed release

If ('publish'! = $contents [' visibility'] | | $contents ['created'] > time ()) {

Return

}

/ / get system configuration

$options = Helper::options ()

/ / determine whether API is configured or not

If (is_null ($options- > plugin ('BaiduSubmitTest')-> api)) {

Return

}

/ / get the article type

$type = $contents ['type']

/ / get routing information

$routeExists = (NULL! = Typecho_Router::get ($type))

/ / generate a permanent connection

$path_info = $routeExists? Typecho_Router::url ($type, $contents):'#'

$permalink = Typecho_Common::url ($path_info, $options- > index)

}

}

On the typecho plug-in how to write the core code to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Article code plug-in interface success content access core information address key platform more knowledge webmaster article routing page permanent generate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei NVidia Linux OPPO Reno