Home Backend Development PHP Tutorial typecho插件编写教程(一):Hello World_php实例

typecho插件编写教程(一):Hello World_php实例

May 16, 2016 pm 08:14 PM
typecho world plug-in write

最近老高正在编写一个关于typecho的插件,由于typecho不像wordpress,有那么多的文档参考,写一个插件还是遇到了很多的坑,不过随着研究的不断深入,老高也慢慢上手了,于是总结出此篇编写教程分享给大家!

I. 从HelloWorld说起

基本信息

想必想要开发typecho的你一定阅读过官方示例插件HelloWorld的源码吧?

我们先看看usr/plugins/HelloWorld/Plugin.php文件前几行

复制代码 代码如下:

if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
 * Hello World
 *
 * @package HelloWorld
 * @author qining
 * @version 1.0.0
 * @link http://typecho.org
 */
 ...
 ...

这几行代码是一个插件的基本信息,我们由代码可以得出以下与插件相关的基本信息

插件说明 ---> Hello World
插件包名 ---> HelloWorld
插件作者 ---> qining
插件版本 ---> 1.0.0
插件链接 ---> http://typecho.org

同时这些信息都会显示在插件页中,如下图

插件结构

我们继续向后面的代码看,一个最简单的插件结构如下(为了缩短篇幅,老高移除了具体方法的实现)

每个方法基本都有注释,老高不再赘述。

看起来很简单吧?其实里面还是有不少坑的。

复制代码 代码如下:

class HelloWorld_Plugin implements Typecho_Plugin_Interface
{
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     *
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate(){}

    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     *
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate(){}

    /**
     * 获取插件配置面板
     *
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form){}

    /**
     * 个人用户的配置面板
     *
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form){}

    /**
     * 插件实现方法
     *
     * @access public
     * @return void
     */
    public static function render(){}
}

插件流程

插件的基本流程是这样的。

1.当我们的插件写好后会出现在后台
2.点击启用按钮后,会执行对应插件类的activate方法
3.插件与目标插件点关联,等待触发
4.当点击停用的时候调用deactivate方法

本节完。

下一节老高会更详细的说明插件类的方法。

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation! PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation! Feb 25, 2024 pm 11:57 PM

PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation!

Error loading plugin in Illustrator [Fixed] Error loading plugin in Illustrator [Fixed] Feb 19, 2024 pm 12:00 PM

Error loading plugin in Illustrator [Fixed]

What is the Chrome plug-in extension installation directory? What is the Chrome plug-in extension installation directory? Mar 08, 2024 am 08:55 AM

What is the Chrome plug-in extension installation directory?

Share three solutions to why Edge browser does not support this plug-in Share three solutions to why Edge browser does not support this plug-in Mar 13, 2024 pm 04:34 PM

Share three solutions to why Edge browser does not support this plug-in

Does PyCharm Community Edition support enough plugins? Does PyCharm Community Edition support enough plugins? Feb 20, 2024 pm 04:42 PM

Does PyCharm Community Edition support enough plugins?

Write a method to calculate power function in C language Write a method to calculate power function in C language Feb 19, 2024 pm 01:00 PM

Write a method to calculate power function in C language

Where is the pycharm plug-in? Where is the pycharm plug-in? Dec 04, 2023 pm 03:39 PM

Where is the pycharm plug-in?

How to write Bloom filter algorithm using C# How to write Bloom filter algorithm using C# Sep 21, 2023 am 10:24 AM

How to write Bloom filter algorithm using C#

See all articles