Hello There!

Lets Connect

Contact Form Demo

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.

  1. Navigate to the WordPress installation’s wp-content directory.
  2. Open the plugins directory.
  3. Create a new directory and name it after the plugin (e.g. plugin-name).
  4. Open the new plugin’s directory.
  5. 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 :

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' );

Leave a Reply

Your email address will not be published. Required fields are marked *