
Plugin Basics
Getting Started
At its simplest, a WordPress plugin is a PHP file with a WordPress plugin header comment. It’s highly recommended that you create a directory to hold your plugin so that all of your plugin’s files are neatly organized in one place.
To get started creating a new plugin, follow the steps below.
- Navigate to the WordPress installation’s wp-content directory.
- Open the plugins directory.
- Create a new directory and name it after the plugin (e.g.
plugin-name
). - Open the new plugin’s directory.
- Create a new PHP file (it’s also good to name this file after your plugin, e.g.
plugin-name.php
).
Header Requirements
As described in Getting Started, the main PHP file should include a header comment that tells WordPress that a file is a plugin and provides information about the plugin.
/*
* 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
*/
Activation / Deactivation Hooks
Activation and deactivation hooks provide ways to perform actions when plugins are activated or deactivated.
- On activation, plugins can run a routine to add rewrite rules, add custom database tables, or set default option values.
- On deactivation, plugins can run a routine to remove temporary data such as cache and temp files and directories.
For Activation :
- To set up an activation hook, use the register_activation_hook() function:
register_activation_hook( string $file, callable $callback )
Parameters
$file string required
, The filename of the plugin including the path.
$callback
callable required, The function hooked to the 'activate_PLUGIN'
action.
register_activation_hook(
__FILE__,
'pluginprefix_function_to_run'
);
Deactivation
To set up a deactivation hook, use the register_deactivation_hook() function:
register_deactivation_hook(
__FILE__,
'pluginprefix_function_to_run'
);
Example For Activation and Deactivation
/**
* Register the "book" custom post type
*/
function pluginprefix_setup_post_type() {
register_post_type( 'book', ['public' => true ] );
}
add_action( 'init', 'pluginprefix_setup_post_type' );
/**
* Activate the plugin.
*/
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' );

kumar
The Managing Director oversees the overall operations and strategic direction of the organization.
Category
- .NET (3)
- Blog (2)
- Common Technology (1)
- CSS (1)
- Topics (1)
- HTML (4)
- Basic HTML (4)
- Javascript (1)
- Topics (1)
- Laravel (32)
- Php (153)
- PHP Arrays (9)
- Php Basic (1)
- PHP interview (152)
- PHP Strings (12)
- Python (15)
- Python interview (13)
- React.js (32)
- React Js Project (3)
- Uncategorized (118)
- WordPress (114)
- Custom Post Types (7)
- Plugin Development (12)
- Theme Customizer (3)
- Theme Development (1)
- Troubleshooting (9)
- WordPress Customization (13)
- Wordpress Database (10)
- WordPress Example (5)
- WordPress Multisite (9)
- Wordpress Project (3)
- WordPress REST API (14)
- WordPress SEO (10)
Recent Posts
Tags
