• Plugins are packages of code that extend the core functionality of WordPress. WordPress plugins are made up of PHP code and can include other assets such as images, CSS, and JavaScript.
  • FileDoc comment example:
/**
 * Get all image nodes.
 *
 * @param \DOMNode     $node       The \DOMDocument instance
 * @param boolean      $strict     If the document has to be valid
 *
 * @return \DOMNode
 */
 public function getImageNodes(\DOMNode $node, $strict = true): \DOMNode
 {
     // ...
 }

Example: Hello Dolly Plugin

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

  • A WordPress installation is a website/project that is built using WordPress. It is a folder on a server (or your local machine) that contains files like:
# example files in wordpress project
wp-admin/
wp-content/
wp-includes/
wp-config.php
index.php

# Create your plugin here
your-wordpress-site/
└── wp-content/
    └── plugins/
        └── plugin-name/
            └── plugin-name.php
  • Header format. Simple and extended:
/*
 * Plugin Name: YOUR PLUGIN NAME
 */
/**
 * Plugin Name
 *
 * @package           PluginPackage
 * @author            Your Name
 * @copyright         2019 Your Name or Company Name
 * @license           GPL-2.0-or-later
 *
 * @wordpress-plugin
 * Plugin Name:       Plugin Name
 * Plugin URI:        https://example.com/plugin-name
 * Description:       Description of the plugin.
 * Version:           1.0.0
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            Your Name
 * Author URI:        https://example.com
 * Text Domain:       plugin-slug
 * License:           GPL v2 or later
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Update URI:        https://example.com/my-plugin/
 * Requires Plugins:  my-plugin, yet-another-plugin
 */
  • Activation and deactivation hooks let your plugin react to being turned on or off, so it can set itself up properly and clean up after itself — without doing unnecessary work every time WordPress runs.
  • The deactivation hook is sometimes confused with the uninstall hook. The uninstall hook is best suited to delete all data permanently such as deleting plugin options and custom tables, etc.
register_activation_hook(
	__FILE__,
	'pluginprefix_function_to_run'
);

register_deactivation_hook(
	__FILE__,
	'pluginprefix_function_to_run'
);

# first parameter in each of these functions refers to your main plugin file
  • PHP provides a number of functions to verify existence of variables, functions, classes and constants. All of these will return true if the entity exists.
  • The root level of your plugin directory should contain your plugin-name.php file and, optionally, your uninstall.php file. All other files should be organized into sub folders whenever possible.
/plugin-name
     plugin-name.php
     uninstall.php
     /languages
     /includes
     /admin
          /js
          /css
          /images
     /public
          /js
          /css
          /images
  • __FILE__ is a PHP magic constant. It resolves to the full filesystem path of the current PHP file at runtime.
  • Authorization checking:
# Example of a core wordpress fn to chck permission
current_user_can( 'edit_others_posts' );

i18n and l10n

  • i18n refers to preparing a theme or plugin so it can be easily translated into multiple languages. This involves wrapping all text strings in translation functions (__(), _e(), _x(), etc.) so they can later be translated without editing the code.
  • 10n stands for “localization” (10 letters between “l” and “n”). This is the process of translating and adapting a theme/plugin for a specific language or region. It uses the internationalization groundwork (i18n) to actually provide translated content to users.
Internationalisation (i18n)Localisation (l10n)
Product design & architectureLanguage & cultural adaptation
One-time (mostly) effortRepeated for each locale
Done by developersDone by translators & local experts
Makes localisation possibleUses internationalisation
  • __('Settings saved', 'super-plugin'); : translates string for localisation. It is core wordpress function.

  • A locale is a language and regional setting used for translations and formatting. It includes the language, country, and sometimes variant. Examples:

    • Gujaratigu_IN
    • Frenchfr_FR
    • Germande_DE
  • WordPress uses two main types of translation files:

    1. .po (Portable Object)

      • Human-readable text file.
      • Contains original strings and their translations.
      • Example entry: msgid "Read More" msgstr "Lire la suite"
    2. .mo (Machine Object)

      • Compiled version of .po.
      • Used by WordPress at runtime.
      • Faster to load, not meant to be edited manually.

The workflow: edit .po → generate .mo → WordPress uses .mo.

Steps to make a theme i18n-ready

  1. Wrap all strings in translation functions:
	<?php _e('Read More', 'my-theme'); ?> 
	<?php __('Hello World', 'my-theme'); ?>

'my-theme' is your text domain (unique identifier for your theme).

  1. Load the text domain in functions.php:
	function my_theme_setup() {
		 load_theme_textdomain('my-theme', 
		 get_template_directory() . '/languages');
	}
	add_action('after_setup_theme', 'my_theme_setup');
  1. Prepare a languages folder in your theme to store .po and .mo files.
  2. Use proper gettext functions: _e(), __(), _n(), _x(), _ex() for plurals and context.
    • .pot (Portable Object Template) is a template file.
    • It contains all the translatable strings from your theme or plugin, but without translations (msgstr is empty).