WordPress插件开发基础-新手入门1

一、WordPress插件开发第一步是建立一个插件的文件夹。步骤如下:

1、导航至WorPress的安装目录下的wp-content文件夹。

2、打开plugins文件,Wordpress所有的插件都在这个文件夹下面。

3、新建一个文件夹并对其命名,文件夹的名字就是你插件的名字,如wpeve。

4、打开这个文件夹。

5、新建一个PHP文件,如wpeve.php。

Unix或linux终端大约如下样子:

wordpress $ cd wp-content
wp-content $ cd plugins
plugins $ mkdir wpeve
plugins $ cd wpeve
plugin-name $ vi wpeve.php

二、在wpeve.php最上面用注释行对插件设置一些基本信息,如插件名称,插件网址、插件描述,插件版本等。这些未必全都要,但至少需要一个插件的名称。

/**
 * Plugin Name: 你插件的名字
 */

涉及到的字段有

Plugin Name:插件名称

Plugin URI:插件的网址,可以是你自己的网站页面,如https://www.wpeve.com,但不能是wordpress.org

Description:插件描述

Requires at least:WordPress最低要求版本

Requires PHP:php最低版本要求

Author:插件作者的名字

Author URI:插件作者的网址

License:插件授权,如GPLv2

License URI:全部授权文字链接

Text Domain:本地化

Domain Path:本地化路径,如/languages

Network:插件是否只能在网络范围内激活。 只能设置为 true,不需要时应省略。

Update URI:第三方更新路径

两个示例

/**
 * Plugin Name:       My Basics Plugin
 * Plugin URI:        https://example.com/plugins/the-basics/
 * Description:       Handle the basics with this plugin.
 * Version:           1.10.3
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            John Smith
 * Author URI:        https://author.example.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Update URI:        https://example.com/my-plugin/
 * Text Domain:       my-basics-plugin
 * Domain Path:       /languages
 */
/**
 * Plugin Name
 *
 * @package           PluginPackage
 * @author            Your Name
 * @copyright         2019 Your Name or Company Name
 * @license           GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       Plugin Name
 * Plugin URI:        https://example.com/plugin-name
 * Description:       Description of the plugin.
 * Version:           1.0.0
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            Your Name
 * Author URI:        https://example.com
 * Text Domain:       plugin-slug
 * License:           GPL v2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Update URI:        https://example.com/my-plugin/
 */

授权示例:

/*
{Plugin Name} is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
 
{Plugin Name} is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with {Plugin Name}. If not, see {URI to Plugin License}.
*/

三、插件的启用(Activation)与禁用(Deactivation)

插件的启用与禁用通过相关的钩子完成(wordpress内的功能函数)

启用:register_activation_hook() 

禁用:register_deactivation_hook()

启用示例:

/**
 * 注册一个“book”的自定义类型
 */
function pluginprefix_setup_post_type() {
    register_post_type( 'book', ['public' => true ] ); 
} 
add_action( 'init', 'pluginprefix_setup_post_type' );
 
 
/**
 * 激活插件.
 */
function pluginprefix_activate() { 
    // Trigger our function that registers the custom post type plugin.
    pluginprefix_setup_post_type(); 
    // Clear the permalinks after the post type has been registered.
    flush_rewrite_rules(); 
}
register_activation_hook( __FILE__, 'pluginprefix_activate' );

禁用示例:

/**
 * Deactivation hook.
 */
function pluginprefix_deactivate() {
    // Unregister the post type, so the rules are no longer in memory.
    unregister_post_type( 'book' );
    // Clear the permalinks to remove our post type's rules from the database.
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'pluginprefix_deactivate' );

插件的卸载,卸载不用于禁用,两者有如下区别:

操作Deactivation Hook(禁用钩子)Uninstall Hook(卸载钩子)
清除缓存和临时文件
更新固定链接
从 {$wpdb->prefix}_options表中清除相关数据
删除数据表

卸载有两种方式:

方式一、register_uninstall_hook

register_uninstall_hook(__FILE__, 'pluginprefix_function_to_run');

方式二、uninstall.php

在插件根目录新建uninstall.php,在插件被删除的时候这个文件将自动运行。

文件运行时,先会检验WP_UNINSTALL_PLUGIN常量。

示例:

// if uninstall.php is not called by WordPress, die
if (!defined('WP_UNINSTALL_PLUGIN')) {
    die;
}
 
$option_name = 'wporg_option';
 
delete_option($option_name);
 
// for site options in Multisite
delete_site_option($option_name);
 
// drop a custom database table
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}mytable");

注意,在多站点中删除插件,遍历所有博客以删除选项可能会占用大量资源。

发表回复