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 entryyg-footer-copyright.php
file.
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
Throughregister_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","版权信息
"); } //启用插件时调用的方法 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
Throughregister_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, theuninstall.php
file will be found in the plug-in directory and the methods in it will be called.
Here we’d better add in theuninstall.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.