Speed up Development Using the WordPress Plugin Boilerplate
Beginner of WordPress Plugin Development: Using WordPress Plugin Boiler
WordPress plug-in development has a low learning curve and there is no only way to build plug-ins. Plugins can be as simple as Hello Dolly, just a single file; they can also be built in a very complex way based on various needs and functions. The WordPress plugin boilerplate is designed to provide a standardized, high-quality foundation that helps you build your next great plugin. In the first part of this series, we will explore the boilerplate in depth, including the structure of files and folders, as well as the code organization of the boilerplate.
Key Points
- WordPress plugin boilerplate provides a standardized, high-quality foundation for building WordPress plugins, with a low learning curve, and can be flexibly used for simple or complex plugin structures.
- The 3.0.0 version of the boilerplate has brought significant improvements in the separation of code organization and internal concerns between plug-ins and can be easily obtained by cloning the git repository or downloading zip files from the GitHub repository.
- The boilerplate follows recommended WordPress coding and documentation standards, provides useful starter guides and tool recommendations, and includes five default classes for a variety of features, making it an excellent starting point for plugin developers.
Background
The WordPress plugin boilerplate was originally initiated by Tom McFarlin to provide a standardized object-oriented approach to building WordPress plugins. Because boilerplate is built on top of the plug-in API and follows the coding and documentation standards recommended by WordPress, you can be sure that you are in a safe and reliable environment. The boilerplate has recently been updated to version 3.0.0, bringing significant improvements in code organization and further separation of internal focus of plugins. If you are already familiar with developing plugins with legacy boilerplate (v2.6.2), it may take some time to relearn what’s new in the latest version, including the new folder structure. This boilerplate is no longer maintained by Tom McFarlin himself (at the time of writing), and he has passed the baton to Devin Vinson. Development is still continuing as planned, so feel free to report it in your GitHub repository if you have any questions. We also welcome your contribution.
Get a template copy
The easiest way to get a boilerplate copy of the WordPress plugin is to clone the git repository itself.
<code>git clone https://github.com/DevinVinson/WordPress-Plugin-Boilerplate.git</code>
Or you can always download zip files from your GitHub repository. Since version 3.0.0 is released, you cannot simply copy the plugin directly into your wp-content/plugins directory. This is because the actual source code of the plugin is not included in the root directory, but is actually in a subdirectory called trunk. Don't worry about this now, we'll discuss the new folder structure later in this article. The README.md file details a complete description of how to properly install the boilerplate. In short, you can:
- Copy the trunk directory to the wp-content/plugins folder
- Copy the entire boilerplate directory and create a symbolic link to wp-content/plugins/
/ for the trunk subdirectory. Creating a symbolic link will depend on the operating system you are using.
After you have done this, you should now find a plugin called WordPress Plugin Boilerplate in the list of plugins in the admin panel, assuming you have not made any modifications to the boilerplate itself. Just activate the plugin and get started!
Customize plug-in with online generator
When activated, you will have a "normal" plugin that basically does nothing -- at least for now. You also need to run search and replace operations on the entire boilerplate code base, which can be very tedious and time-consuming. Fortunately, Enrique Chavez created an unofficial generator that automates the process. Simply visit https://www.php.cn/link/6340831392e9cd8af9598274f4b3c5c0 and fill in the form at the bottom of the page to generate a personalized copy of the WordPress plugin template.
Folder structure
Let's take a closer look at how WordPress plugin boilerplate 3.0.0 is organized compared to older versions. As mentioned earlier, the actual plugin code is contained in a subdirectory called trunk, not in the root directory. This is to follow the standards used in the official WordPress plugin repository, which contains three main directories: assets, branches, and trunk. The boilerplate already provides two of them: assets and trunk. Here are the complete directories and files included in the boilerplate (at the time of writing):
<code>git clone https://github.com/DevinVinson/WordPress-Plugin-Boilerplate.git</code>
The following is a brief description of the functions of each file and folder:
-
.gitignore
: Provides a reasonable default.gitignore
for most content that should not exist in the git repository. -
CHANGELOG.md
: Standard change log for boilerplate changes, including the change date. -
README.md
: A useful starter with the installation instructions listed, along with some other parts such as tool recommendations and acknowledgments. -
assets
: This directory contains recommended resources you need to provide when you decide to publish the plugin to the WordPress plugin repository. All images contained in this directory are of the recommended resolution for publication. -
trunk
: This is the actual plugin you are going to develop. There are some folders that divide the code base into administrative and public-facing features. We will explain in detail what each subdirectory means.-
admin
: The directory contains three directories, namelyadmin
,css
andjs
. As the name implies, all management-oriented features should be placed here. By default,partials
andplugin-name-admin.js
are added to your wp-admin.plugin-name-admin.css
will provide common features where you can define management-specific hooks.class-plugin-name-admin.php
- : This directory is very similar to the functionality provided by the
public
directory, the only difference is that theadmin
directory is used to store all public-facing functional code bases.public
- : A starting
languages
file where you can provide translation functionality for plugins..pot
- : This is probably where almost everything magic happens. By default, there are five starting classes, which we will discuss in the next section.
includes
- : Contains a copy of the GPL v2 license by default.
LICENSE.txt
- : The starting point of the plug-in README file. This file covers all sections that you can further fill in to provide a good plugin page in the WordPress plugin repository.
README.txt
- : The entry point of the plug-in. Here, a common plugin file header is included that you can modify according to your preferences. If you need to include a feature when the plugin is activated and/or deactivated,
plugin-name.php
andregister_activation_hook
will also be registered in this file.register_deactivation_hook
-
Included classes
As mentioned earlier, there are five default classes in the directory. Let's see what each class does: trunk/includes
-
class-plugin-name-activator.php
: This class is instantiated during plug-in activation. It has only one static methodactivate()
, which is registered toregister_activation_hook
. Use this class whenever you need to perform certain actions when the plugin is activated, such as creating a custom table or saving default options. -
class-plugin-name-deactivator.php
: The corresponding category ofclass-plugin-name-deactivator.php
. It also has only one static methoddeactivate()
that can be used to run any feature during plugin deactivation. -
class-plugin-name-i18n.php
: The starting point of the plug-in i18n function. It has a property$domain
that stores plugin text fields. This property can be set using the public methodset_domain()
. Finally, whenever the plugin is loaded, theload_plugin_textdomain()
method in this class is called. -
class-plugin-name-loader.php
: The most important class in the template. It contains two properties$actions
and$filters
, where all hooks registered in the plugin will be stored. It provides two simple wrapper functionsadd_action()
andadd_filter()
for adding an action or filter to the$actions
and$filters
properties. This is not to be confused with WordPress's defaultadd_action()
andadd_filter()
functions, because such classes do not actually register them directly. All hooks will only be registered during another method calledrun()
. -
class-plugin-name.php
: A class that glues all parts together. It contains important information about the plugin, such as the plugin name and version. Additionally, it will load the dependencies using theload_dependencies()
method, which will contain the above four classes, and the plugin text field will be set using theset_locale()
method. All administrative and public hooks registered previously can also be defined here. This class also provides simple get methods such asget_plugin_name()
to return the plugin name,get_version()
to return the current plugin version, andget_loader()
to preserve the instance ofclass-plugin-name-loader.php
.
Conclusion
In short, WordPress plugin boilerplate provides a great starting point for plugin developers. It follows the recommended WordPress coding standards as well as WordPress documentation standards, so you know you're on the right path from the start. Additionally, publishing to a WordPress plugin repository becomes easier because boilerplate already provides most of what you need. Once we are familiar with the code organization and folder structure, we will explore how to develop actual plugins using WordPress plugin boilerplate in the second part of this series.
The above is the detailed content of Speed up Development Using the WordPress Plugin Boilerplate. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Blogs are the ideal platform for people to express their opinions, opinions and opinions online. Many newbies are eager to build their own website but are hesitant to worry about technical barriers or cost issues. However, as the platform continues to evolve to meet the capabilities and needs of beginners, it is now starting to become easier than ever. This article will guide you step by step how to build a WordPress blog, from theme selection to using plugins to improve security and performance, helping you create your own website easily. Choose a blog topic and direction Before purchasing a domain name or registering a host, it is best to identify the topics you plan to cover. Personal websites can revolve around travel, cooking, product reviews, music or any hobby that sparks your interests. Focusing on areas you are truly interested in can encourage continuous writing

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

Do you want to know how to display child categories on the parent category archive page? When you customize a classification archive page, you may need to do this to make it more useful to your visitors. In this article, we will show you how to easily display child categories on the parent category archive page. Why do subcategories appear on parent category archive page? By displaying all child categories on the parent category archive page, you can make them less generic and more useful to visitors. For example, if you run a WordPress blog about books and have a taxonomy called "Theme", you can add sub-taxonomy such as "novel", "non-fiction" so that your readers can

Recently, we showed you how to create a personalized experience for users by allowing users to save their favorite posts in a personalized library. You can take personalized results to another level by using their names in some places (i.e., welcome screens). Fortunately, WordPress makes it very easy to get information about logged in users. In this article, we will show you how to retrieve information related to the currently logged in user. We will use the get_currentuserinfo(); function. This can be used anywhere in the theme (header, footer, sidebar, page template, etc.). In order for it to work, the user must be logged in. So we need to use

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

In the past, we have shared how to use the PostExpirator plugin to expire posts in WordPress. Well, when creating the activity list website, we found this plugin to be very useful. We can easily delete expired activity lists. Secondly, thanks to this plugin, it is also very easy to sort posts by post expiration date. In this article, we will show you how to sort posts by post expiration date in WordPress. Updated code to reflect changes in the plugin to change the custom field name. Thanks Tajim for letting us know in the comments. In our specific project, we use events as custom post types. Now

One of our users asked other websites how to display the number of queries and page loading time in the footer. You often see this in the footer of your website, and it may display something like: "64 queries in 1.248 seconds". In this article, we will show you how to display the number of queries and page loading time in WordPress. Just paste the following code anywhere you like in the theme file (e.g. footer.php). queriesin

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.
