WordPress Development Basic
What is a Plugin?
Plugins are packages which help us improve/extend the core functionalities of WordPress. Plugins are defined with a main header file which contains the plugin header in the format below
/**
* 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
*/In these header settings, the main required header which is required to validate a plugin file is Plugin Name
Do not touch WordPress core.
We have to always add new functionality to plugins instead of WordPress core files as WordPress core files can change with the new updated and our changes would be lost if something like that happens.
Creating a demo plugin
To create a demo plugin that would show up in the plugins menu, we need to create a new folder in our wp-content/plugins directory
- For e.g. we will create a folder named
demo-plugin - Inside this demo plugin directory we would create a file named
demo-plugin.php - We will add the following header code to it.
/**
* Plugin Name: Demo Plugin
*/On saving this file and refreshing our back-end plugin page, we would see our newly created plugin named Demo Plugin in the table.
- Only one file should have the plugin header else we would be getting multiple plugin entries in the table
Hooks
Hooks allow us to add or change functionality in WordPress without changing the core wordpress files.
There are two type of hooks
- Action Hook
- Filter Hook
Actions hooks allows us to add/change the functionality, or when some action is triggered how WordPress should behave
Filter hooks allow us to add/modify the data before it is being sent to the dataabase or the browser.
register_activation_hook()
it is run when a plugin is activated, we can use it to initialize our plugin options and data to the database
register_deactivation_hook()
it is run when a plugin is deactivated from the WP dashboard, we can use it to clear any temporary data stored by our plugin
register_uninstall_hook()
it is run when a plugin is uninstalled, we can use it to remove the extra options or data we added to the database when initializing the plugin, which is no longer required
Mid day questions
Q : Does the deactivation hook get executed when you uninstall a plugin? Why?
A: The deactivation hook only executes when the user deactivates a plugin, before uninstalling.
The uninstallation hook gets executed when the user uninstall a plugin, so that the uninstallation hook can clear the options data from the database which is no longer required.
Q: What is the minimum plugin header information required for a plugin to be recognized as a valid plugin?
Just the plugin name is the minimum requirement for a plugin to be recognized as a valid plugin
/**
* Plugin Name: YOUR PLUGIN NAME
*/
Q: Can the main plugin file be placed in a nested folder? (my-plugin/dummy-folder/main-file.php)
A: No, the main plugin file cannot be placed in a nested folder, it must be placed in the plugin folder only.
As wordpress searches for the file in the plugin directory only
Q: Can you rename the wp-content folder? If yes, would you be required to configure anything to get it working after the change? If no, write 'Nahi kr skte'.
A: Yes we can rename the wp-content folder.
Rename the folder to another name, for e.g. new-wp-contentOpen wp-config.php and add these configurations
define( 'WP_CONTENT_DIR', ABSPATH . 'new-wp-content' ); // Provide path to the new wp content folder
define( 'WP_CONTENT_URL', 'http://localhost:10033/new-wp-content/' ); // Provide url to the new wp content folder