Plugin

WordPress consists of three main components: Core, Themes, Plugins. In this section, we will talk about plugins, explore how to create a plugin for WordPress, and touch on the following topics:

This section is created based on the official plugin guide. Here will be a free translation + personal experience.

What is a plugin?

WordPress is designed in such a way that it can be easely extended with plugins.

Plugin is code package that enhance WordPress's core features. It primarily consist of PHP code and may also include assets like images, CSS, and JavaScript.

The possibilities of plugins are limitless, for example:

  • You can create a plugin that adds a new field for comments and will record data in the wp_commentmeta table.

  • Or using custom post types, you can write a ticket system plugin with email notifications, ticket statuses, etc.

  • Or you can greatly extend the capabilities of WordPress by making it an e-commerce system (like the woocommerce plugin).

Why are plugins needed?

Plugins allow you to extend the functionality of WordPress without modifying the core WordPress code.

There is one important rule in WordPress development - do not touch the core. This means that you should not edit the files of WordPress itself when you need to change or add something. This is important because, during updates, WordPress replaces all its files, and any changes to these files will be lost. Therefore, any functionality must be added through plugins, MU plugins, or through a theme (template).

What does a plugin consist of?

In its simplest form, a WordPress plugin is a single PHP file with special header comments. The minimum required for a plugin is a correct header in the single main file and code that will do something.

However, more often than not, a plugin consists of several files.

After installing WordPress, there is a demo plugin in the plugins folder: Hello Dolly. This plugin consists of a single file /plugins/hello-dolly/hello.php. "Hello Dolly" does nothing useful, it simply displays words from a famous song in the WordPress admin notifications.

Here is its code:

GitHub
<?php
/**
 * @package Hello_Dolly
 * @version 1.7.2
 */
/*
Plugin Name: Hello Dolly
Plugin URI: http://wordpress.org/plugins/hello-dolly/
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.
Author: Matt Mullenweg
Version: 1.7.2
Author URI: http://ma.tt/
*/

function hello_dolly_get_lyric() {
	/** These are the lyrics to Hello Dolly */
	$lyrics = "Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
I feel the room swayin'
While the band's playin'
One of our old favorite songs from way back when
So, take her wrap, fellas
Dolly, never go away again
Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
I feel the room swayin'
While the band's playin'
One of our old favorite songs from way back when
So, golly, gee, fellas
Have a little faith in me, fellas
Dolly, never go away
Promise, you'll never go away
Dolly'll never go away again";

	// Here we split it into lines.
	$lyrics = explode( "\n", $lyrics );

	// And then randomly choose a line.
	return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] );
}

// This just echoes the chosen line, we'll position it later.
function hello_dolly() {
	$chosen = hello_dolly_get_lyric();
	$lang   = '';
	if ( 'en_' !== substr( get_user_locale(), 0, 3 ) ) {
		$lang = ' lang="en"';
	}

	printf(
		'<p id="dolly"><span class="screen-reader-text">%s </span><span dir="ltr"%s>%s</span></p>',
		__( 'Quote from Hello Dolly song, by Jerry Herman:', 'hello-dolly' ),
		$lang,
		$chosen
	);
}

// Now we set that function up to execute when the admin_notices action is called.
add_action( 'admin_notices', 'hello_dolly' );

// We need some CSS to position the paragraph.
function dolly_css() {
	echo "
	<style type='text/css'>
	#dolly {
		float: right;
		padding: 5px 10px;
		margin: 0;
		font-size: 12px;
		line-height: 1.6666;
	}
	.rtl #dolly {
		float: left;
	}
	.block-editor-page #dolly {
		display: none;
	}
	@media screen and (max-width: 782px) {
		#dolly,
		.rtl #dolly {
			float: none;
			padding-left: 0;
			padding-right: 0;
		}
	}
	</style>
	";
}

add_action( 'admin_head', 'dolly_css' );

Plugin Load

Don't believe it when you read somewhere that plugins overload the system - that is not true!

Activating a plugin is no different in terms of load than including a regular PHP file. Therefore, a well-crafted plugin cannot load the system more than if you included the same code directly in PHP.

The myth that plugins load the system arose because many WordPress plugins are poorly written by beginners. But that does not mean that all plugins create excessive load. You just need to use plugins from experienced/good developers.

For a technical explanation of why plugins do not load the system, read the note: “Do plugins slow down WordPress?”

--

This section is incomplete. Here are only the main things related to creating a plugin.