Home  >  Article  >  CMS Tutorial  >  Perform WordPress plug-in development - create, deactivate, delete plug-ins

Perform WordPress plug-in development - create, deactivate, delete plug-ins

藏色散人
藏色散人forward
2021-02-23 15:32:223212browse

The following tutorial column of WordPress will introduce you to WordPress plug-in development-creating, deactivating, and deleting plug-ins. I hope it will be helpful to friends in need!

Perform WordPress plug-in development - create, deactivate, delete plug-ins

Plug-in storage directory

wp-content/plugins

Create a plug-in

Create in plugins For a file plug-in folder, it is best to add a prefix to the name. This prefix can use your name or your own domain name to prevent the plug-in from having the same name as others. Then create a PHP file with the same name as your plug-in.
I create a plug-in called yg-footer-copyright here.

Let WordPress recognize our plug-in

After creating the plug-in, the WordPress backend cannot recognize our plug-in. That is because we did not write the plug-in information according to its standards.

Write the plug-in information in the header of your plug-in entry yg-footer-copyright.php file.

<?php
/*
Plugin Name: 插件名称
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: 插件的简单描述
Version: 插件版本号, 例如: 1.0
Author: 插件作者
Author URI: http://URI_Of_The_Plugin_Author作者地址
*/
?>

At this time, you can see the plug-in you created by looking at the WordPress backend.

Method called when the plug-in is enabled

Through register_activation_hook this method can add a callback when the plug-in is enabled.

Official document: https://codex.wordpress.org/F...

function ygcopyright_install() {
    update_option("yg-copyright","<p>版权信息</p>");
}
//启用插件时调用的方法
register_activation_hook( __FILE__, 'ygcopyright_install' );

Here we add a yg-copyright field in the option table at startup.

Method called when the plug-in is deactivated

Through register_deactivation_hook this method can add a callback when the plug-in is deactivated.

Official document: https://codex.wordpress.org/F...

function ygcopyright_stop(){
    update_option("yg-copyright","yes");
} 
//停用插件时的方法
register_deactivation_hook( __FILE__, 'ygcopyright_stop' );

Here we change the yg-copyright field in the option table to yes when deactivating.

Operation when deleting the plug-in

When the plug-in is deleted, by default, the uninstall.php file will be found in the plug-in directory and the methods in it will be called.

<?php
//判断是不是从WordPress后台调用的
if(!defined("WP_UNINSTALL_PLUGIN"))
exit();

delete_option("yg-copyright");
?>

Here we’d better add in the uninstall.php file header to determine whether it is called by WordPress background, to prevent others from calling this file directly and delete the plug-in.
Here we delete the yg-copyright field in the option table when deactivating.

If you have any questions, please leave a message.

The above is the detailed content of Perform WordPress plug-in development - create, deactivate, delete plug-ins. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete