WordPress is one of the most popular content management systems (CMS) in the world. One of the best features of WordPress is the ability to create your own plugins and thus customize and extend the functionality of your website as you wish. In this blog post, I will give you a detailed guide on how to create a WordPress plugin that integrates multiple functions, has its own administration page and offers full activation and deactivation features.

Step 1: Understand Plugin Basics

Before we start with the actual development, we need to understand the basics of a WordPress plugin. A plugin is essentially a collection of PHP files that are placed in the WordPress directory wp-content/plugins It extends the functionality of your website and can perform various tasks, from customizing the theme to adding new features.

Step 2: Create plugin structure

Let’s start by creating the basic structure of our plugin. Create a new folder in wp-content/plugins and name it “my-plugin”. In this folder, create a main file called mein-plugin.php. This file is loaded when the plugin is activated and contains the code that initializes the plugin's functions.

<?php
/*
Plugin Name: Mein WordPress Plugin
Description: Ein tolles Plugin für deine Website.
Version: 1.0
Author: Dein Name
*/

// Hier beginnt der Plugin-Code

 

?>

Step 3: Add features

Now let's add some example functionality. Suppose we want to add a custom button to the WordPress editor to insert special content. Here is a simple example:

function mein_plugin_button($buttons) {
array_push($buttons, "mein_plugin_button");
return $buttons;
}

function mein_plugin_button_action($content) {
return $content . "<p>Hallo, das ist mein Plugin!</p>";
}

add_filter('mce_buttons', 'mein_plugin_button');
add_filter('mce_external_plugins', 'mein_plugin_button_action');

If you want to have multiple features in your plugin, there are a few things you should pay attention to:

1. Uniqueness of function names: Avoid using generic names for your functions as they may clash with other plugins or themes. Instead, use prefixes to ensure they are unique (eg myplugin_my_function()).

2. Order and structure: Organize your code by grouping related functions into separate files or sections of your main file. This makes your code more manageable and easier to maintain.

3. Comments: Comment your code, especially if it is complex or unclear. This will help you and other developers understand the code later.

4. Use actions and filters: Instead of directly modifying WordPress core code, use actions and filters to make changes. This will ensure that your plugin remains compatible with future WordPress updates.

5. Checking for existence of functions: If your plugin depends on certain features that might be added or removed in later WordPress versions, always check first if that feature exists before calling it:

if (function_exists('die_funktion')) {
die_funktion();
}

6. Testing: Before releasing your plugin, you should test it thoroughly, especially in combination with other popular plugins and themes, to ensure there are no conflicts or unwanted side effects.

Step 4: Create your own administration page (optional)

Another common use case for plugins is to create your own administration page. Here is a simple example of how you can do that:

function mein_plugin_admin_page() {
add_menu_page(
'Mein Plugin Einstellungen',
'Mein Plugin',
'manage_options',
'mein-plugin-admin',
'mein_plugin_admin_page_callback'
);
}

function mein_plugin_admin_page_callback() {
echo '<div class="wrap">';
echo '<h2>Mein Plugin Einstellungen</h2>';
// Hier kannst du den Inhalt deiner Administrationsseite einfügen.
echo '</div>';
}

add_action('admin_menu', 'mein_plugin_admin_page');

4.1. The function mein_plugin_admin_page() create

First we create a function called mein_plugin_admin_page(). This function is used to add the administration page for our plugin. The add_menu_page()function is used to add a menu item in the WordPress admin interface.

function mein_plugin_admin_page() {
add_menu_page(
'Mein Plugin Einstellungen', // Seitentitel in der Menüleiste
'Mein Plugin', // Anzeigename im Menü
'manage_options', // Benutzerrolle, die Zugriff hat (hier: Administratoren)
'mein-plugin-admin', // Eindeutiger Seiten-Slug
'mein_plugin_admin_page_callback' // Callback-Funktion zum Rendern der Seite
);
}

'My Plugin Settings' is the title that appears in the WordPress administration menu bar.
'My Plugin' is the display name in the menu.
'manage_options' specifies that only users with the Administrator role have access to this page.
'my-plugin-admin' is a unique slug for your administration page.
'my_plugin_admin_page_callback' is the callback function that is called to create the content of the administration page.

4.2. The callback function my_plugin_admin_page_callback() create:

The callback function my_plugin_admin_page_callback() is called to create the content of the administration page. Here you can add all the settings and options for your plugin. For example:

function mein_plugin_admin_page_callback() {
echo '<div class="wrap">';
echo '<h2>Mein Plugin Einstellungen</h2>';
// Hier kannst du den Inhalt deiner Administrationsseite hinzufügen.
echo '</div>';
}

'<div class="wrap">' is used to wrap the content of the page and apply the standard WordPress styling.
'<h2>My Plugin Settings</h2>' is the title of the administration page. You can customize this title as you wish.
In the line // Here you can add the content of your administration page. you can add custom HTML forms, input fields and options to control your plugin's settings.

4.3. Add the administration page

To add your administration page, go to the mein_plugin_admin_page()function and attach it to the action admin_menu. This is usually done in the main file of your plugin.

add_action('admin_menu', 'mein_plugin_admin_page');

This line ensures that the function mein_plugin_admin_page() is called when the WordPress admin menu is created. This will make your plugin visible in the WordPress admin interface.

4.4. Test page

Now you can activate your plugin and test the administration page. Go to your WordPress dashboard, click on the “My Plugin” menu item (or your chosen display name) and you will be redirected to the administration page where you can add your settings and features.

Creating your own administration page allows users to customize your plugin's configuration and makes the overall user experience more user-friendly. You can use this page to add options, settings, or other plugin-specific content that you want users to manage.

Step 5: Manage activation and deactivation (optional)

Your plugin should also provide the ability to be activated and deactivated. This is done by adding activation and deactivation functions in the main file of your plugin:

register_activation_hook(__FILE__, 'mein_plugin_activate');
register_deactivation_hook(__FILE__, 'mein_plugin_deactivate');

function mein_plugin_activate() {
// Hier kannst du Initialisierungsaktionen durchführen, wenn das Plugin aktiviert wird.
}

function mein_plugin_deactivate() {
// Hier kannst du Bereinigungsaktionen durchführen, wenn das Plugin deaktiviert wird.
}

Step 6: Install the finished plugin

Once you have finished programming your plugin and want to integrate it into your WordPress installation, follow these steps:

1. Compile plugin files

  • Your plugin should have a main file that contains the plugin metadata, such as the plugin name, version, author, etc. This metadata will be displayed in the WordPress admin area.
  • Organize all files and directories related to your plugin in a separate folder.

2. Create ZIP archive

  • Pack the folder with your plugin into a ZIP archive. This makes it easier to upload and install in WordPress.

3. Install the plugin in WordPress

Manual installation via the WordPress dashboard

3.1. In the WordPress admin area, go to “Plugins” > “Install”.
3.2. Click on “Upload Plugin” and select the previously created ZIP archive.
3.3. Click on “Install now”.
3.4. After successful installation, click on “Activate plugin”.

Manual installation via FTP

3.1. Unzip the ZIP archive on your computer.
3.2. Use an FTP client (e.g. FileZilla) to connect to your web server.
3.3. Navigate to the directory wp-content/plugins/ your WordPress installation.
3.4. Upload your plugin folder to this directory.
3.5. In your WordPress admin area, go to “Plugins”, find your plugin in the list and click “Activate”.

4. Testing

  • After activating the plugin, you should test its functionality on your website. Make sure that everything works as expected and that there are no conflicts with other plugins or the theme.

5. Updates and Maintenance

  • Whenever you make updates to your plugin, always increment the version number in the plugin metadata header.
  • Once you have a new version, you can repackage the plugin and update it from the dashboard. Some developers also use update management systems to provide updates for their plugins.

Step 7: Publish plugin (optional)

If you want other users to be able to use your plugin, you can publish it to the official [WordPress Plugin Repository](https://wordpress.org/plugins/). To do so, you must create an SVN account, submit your plugin for review, and follow the WordPress Plugin Development Guidelines.

Don't forget to back up your website regularly, especially before installing new plugins or updating them, to prevent potential data loss.

Conclusion

Creating a WordPress plugin may seem complex at first, but with a clear structure and this step-by-step guide, you can develop your own plugins with ease. Remember that security and performance should always be your top priority when creating plugins.