WordPress: Create the “Hello World” plugin yourself

Plugins can be used to extend the functionality of a WordPress installation in many ways. This article describes how you can create the basic structure for your first plugin.

The plugin framework

A WordPress plugin ultimately consists of PHP methods that are added to the “/plugins” folder of our WordPress installation and then automatically executed there. Therefore, we first create a suitable PHP file for our plugin in the “/plugins” folder, preferably already organized in an associated subfolder. The file names can be chosen relatively freely, here I use “cathy-plugin-demo” for example:

   [wordpress]/wp-content/plugins/cathy-plugin-demo/cathy-plugin-demo.php

The properties of a plugin, such as name, description, author and version, must be specified in this PHP file in fixed meta tags within a PHP comment block. For example, for the first version of our own plugin, this comment block in cathy-plugin-demo.php could look like this:

<?php
/*
Plugin Name: Cathy Plugin Demo
Description: WordPress "Hello World"-Plugin-Demo
Version: 0.1
Author: CathyProductions
Author URI: https://www.cathyprod.de
License: GPL
*/
?>

As soon as we save this PHP file in the folder mentioned above, WordPress will already show us the newly found plugin in the plugin area of ​​the administration, and we can already activate it:

The first function

However, our self-made plugin still has no function. A WordPress plugin gets its functionality by hooking on to the specified internal methods that are called when each WordPress page is built, either by executing additional code or by filtering and modifying the data to be processed. This is done using the add_action() and add_filter() commands.

As a demonstration, we add an action and a filter to the cathy-plugin-demo.php, once for the structure of the footer and once for reading each content of a blog entry. Then we program the HTML extension we want to do this in a function of the same name:

<?php
/*
Plugin Name: Cathy Plugin Demo
Description: WordPress "Hello World"-Plugin-Demo
Version: 0.1
Author: CathyProductions
Author URI: https://www.cathyprod.de
License: GPL
*/

add_action('wp_footer', 'cathy_plugin_demo_action_footer');
function cathy_plugin_demo_action_footer() {
  echo '<div>Plugin Demo Add Footer "Hello World"</div>';
}

add_filter('the_content', 'cathy_plugin_demo_filter_content');
function cathy_plugin_demo_filter_content($content) {
  $content .= '<p>Plugin Demo Add One Line - "Hello World"</p>';
  return $content;
}

?>

If we now activate our self-created plugin in the administration, the line Plugin Demo Add Footer “Hello World” is automatically output via the echo function through the action attached to the wp_footer at the bottom of your blog.

In addition, the line Plugin Demo Add One Line – “Hello World” is added at the end of each of your blog posts via the filter method on the_content. The filter appends this text to the WordPress $content variable, as shown in this blog post screenshot:

Wordpress Plugin Hello World result

Source code in GitHub

Related Links:

I like it:

Like Loading…