WordPress Plugin Development Beginner’s Guide: Creating Your First Plugin from Scratch

2-minute read
2026-03-12
2026-06-03
2,214
I earn commissions when you shop through the links below, at no additional cost to you.

Why did you choose to develop a WordPress plugin?

WordPress, as the world's most popular content management system, owes its strong scalability largely to plugins. By developing plugins, you can add any custom functionality to a website without having to modify the core files, which ensures compatibility during WordPress upgrades. Whether it's to meet the needs of a specific project, solve a common problem, or to share your solutions with the community and generate revenue, plugin development is a highly valuable skill.

Learning plugin development not only allows you to gain a deep understanding of the workings of WordPress, such as its Hook system and Shortcode functionality, but it also enhances your PHP programming skills. Everything from simple feature enhancements to complex application integrations is possible.

Setting up your plugin development environment

Before starting to write code, having a suitable local development environment is crucial. It allows you to test and debug your code securely, without affecting the live website.

Recommended Reading The Ultimate Guide to WordPress Plugin Development: Building a Professional Plugin from Scratch

Configuring the local server environment

It is recommended to use integrated local development tools such as Local by Flywheel, DevKinsta, or XAMPP. These tools can be installed with just one click and come pre-installed with Apache/Nginx, PHP, and MySQL. Make sure that your PHP version is compatible with your target server; it is generally recommended to use PHP 7.4 or a later version for better performance and security.

UltaHost WordPress Hosting
30-day refund guarantee, unlimited bandwidth and database usage, free DDoS protection; purchase for 3 years and get a discount of 50%.

Install and set up WordPress.

Install a brand-new WordPress instance on your local server. It is recommended to use the default theme (such as the Twenty Twenty series) for development and testing purposes to avoid any potential theme compatibility issues.wp-config.phpIn the file, enable it.WP_DEBUGConstants will help you quickly identify errors during the development process.

define( 'WP_DEBUG', true );

Prepare a code editor.

Choose a powerful code editor, such as Visual Studio Code, PhpStorm, or Sublime Text. Install relevant extensions, such as PHP IntelliSense and WordPress code snippets, as they can greatly improve your coding efficiency.

Create your first plug-in file

A WordPress plugin is essentially one or more components that are located within the WordPress framework./wp-content/plugins/The PHP files in the directory. Now, let’s start with the most basic structure.

Write the header comments for the plug-in

Every plugin must contain a header comment in a standard format, which WordPress uses to identify the plugin’s information. In your plugin directory (for example…/wp-content/plugins/my-first-plugin/In the document, create a section named…my-first-plugin.phpThe main file.

Recommended Reading From Beginner to Expert: A Complete Guide to WordPress Plugin Development

<?php
/**
 * Plugin Name:       我的第一个插件
 * Plugin URI:        https://example.com/my-first-plugin
 * Description:       这是一个用于学习的简单WordPress插件。
 * Version:           1.0.0
 * Author:            你的名字
 * Author URI:        https://example.com
 * License:           GPL v2 or later
 * Text Domain:       my-first-plugin
 * Domain Path:       /languages
 */

This comment defines the information such as the name and description that the plugin displays in the WordPress administration interface.Text DomainUsed for internationalization and translation.

Building a basic security framework for plugins

To prevent direct access to your plugin files, you can add a security code at the beginning of the file. This is a best practice for WordPress plugin development.

// 防止直接访问
if ( ! defined( 'ABSPATH' ) ) {
	exit; // 如果未定义ABSPATH(WordPress根目录的绝对路径),则退出
}

ABSPATHThis is a constant defined by WordPress, representing the absolute path where it is installed. By checking whether this constant exists, you can effectively prevent external users from directly accessing your plugin files via a URL.

hosting.com Shared Hosting
High performance with AMD EPYC CPUs, NVMe SSD storage and LiteSpeed, 24/7, 24x7 expert in-house support, advanced security measures including SSL, brute force, malware and DDoS protection, savings of up to 73%

Implement the core features: add a management menu and settings.

A complete plugin usually needs to provide a configuration interface in the WordPress backend. We will use the API provided by WordPress to add a simple management menu page.

Use a function to add a top-level management menu.

We will useadd_action()The function is mounted.admin_menuOn the hook, a menu item is added in the background. Continue to add the following code to your main plugin file.

// 添加管理菜单
function mfp_add_admin_menu() {
    add_menu_page(
        '我的第一个插件设置', // 页面标题
        '我的插件',           // 菜单标题
        'manage_options',    // 所需权限(管理员)
        'my-first-plugin',   // 菜单slug
        'mfp_settings_page', // 显示页面内容的回调函数
        'dashicons-admin-generic', // 图标(使用Dashicons)
        80                    // 菜单位置
    );
}
add_action( 'admin_menu', 'mfp_add_admin_menu' );

Here,add_menu_page()The function is used to create a top-level menu. The parameters define the page title, menu title, access permissions, unique identifier (slug), the callback function used to render the page content, the icon, and the position of the menu in the sidebar.

Recommended Reading Complete Guide to WordPress Plugin Development: From Zero to Live Deployment

Create a callback function for the settings page.

Now, we need to define what was mentioned above.mfp_settings_page()This function will output the HTML content for the management page.

// 设置页面回调函数
function mfp_settings_page() {
    // 检查用户权限
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    ?&gt;
    <div class="wrap">
        <h1></h1>
        <p>Welcome to the settings page for my first plugin! This is where you will add forms and options in the future.</p>
        <form action="/en/options.php/" method="post" data-trp-original-action="options.php">
            <?php
            // 输出设置字段、非ce等(后续扩展)
            settings_fields( 'mfp_options_group' );
            do_settings_sections( 'my-first-plugin' );
            submit_button( '保存设置' );
            ?>
        <input type="hidden" name="trp-form-language" value="en"/></form>
    </div>
    &lt;?php
}

This function first checks whether the current user has...manage_optionsPermissions (usually for administrators), followed by the output of a simple setup page framework.settings_fields()anddo_settings_sections()This is reserved for setting up API registration options for subsequent use with WordPress.

InterServer Shared Hosting
Shared hosting $2.50 USD per month , first month $0.1 USD promo code tryinterserver, 461 cloud apps scripts, one click install.

Add a simple front-end feature to the plugin.

In addition to the backend administration interface, plugins are more commonly used to provide functionality for the website’s front end. We will create a simple shortcode that users can use in their articles or pages.

Use a function to register a shortcut code.

Shortcodes allow users to…[shortcode]Such simple tags are used to insert dynamic functionality into the content.add_shortcode()The function is registered.

// 注册一个短代码
function mfp_hello_world_shortcode( $atts ) {
    // 短码属性,设置默认值
    $atts = shortcode_atts(
        array(
            'name' =&gt; '访客',
        ),
        $atts,
        'mfp_hello'
    );

// 安全地输出内容
    return '<p>Hello, '. esc_html($atts['name'])'. ‘! This is the greeting generated by my plugin.</p>'add_shortcode( 'mfp_hello', 'mfp_hello_world_shortcode' );

add_shortcode()The function accepts two parameters: the short code tag name (the name used by users) and the corresponding processing function.shortcode_atts()The function is used to merge the user-provided attributes with default values, ensuring the robustness of the code.

Testing short code on the front end

After saving the plugin file, go to the “Plugins” page in the WordPress administration dashboard and activate “My First Plugin”. Then, create or edit an article, and directly enter the code in the content editor.[mfp_hello name="小明"]After the article is published, the front-end page will display “Hello, Xiaoming! This is a greeting generated by my plugin.” If the `name` attribute is not provided, for example…[mfp_hello]Then the default message “Hello, visitor!” will be displayed.

summarize

By following this guide, you have completed the entire process of creating a basic WordPress plugin from scratch. You have learned about the basic structure of a plugin, the importance of header comments, how to add a backend administration menu, and how to create simple yet useful front-end shortcodes. These are the fundamental building blocks of WordPress plugin development.

The real journey of plugin development has just begun. Next, you can explore WordPress’s powerful settings API to create customizable options that can be saved, and make use of action hooks to extend the functionality of your plugins.add_action()and filter hooksadd_filter()Modify the core functionality, learn how to add internationalization support to plugins, and understand how to handle user data securely. Remember that adhering to WordPress coding standards and prioritizing security are crucial for developing high-quality plugins.

FAQ Frequently Asked Questions

What are the prerequisite knowledge requirements for developing WordPress plugins?

You need to have a basic understanding of PHP programming, knowledge of HTML and CSS, and familiarity with the basic operations of WordPress (such as publishing articles and installing themes/plugins). Familiarity with object-oriented programming (OOP) is not mandatory, but it is very helpful for developing more complex plugins.

Does the main file name of the plugin have to be the same as the folder name?

It’s not mandatory, but it’s a highly recommended best practice that will make the structure clearer. The PHP file of the main plugin can have any name, but its header comments must be correct, and the file must be located within the folder that contains the plugin itself. The folder name is usually the same as the plugin’s slug.

How do I debug my plugin code?

In addition to enabling…WP_DEBUGYou can also use…error_log()The function writes debugging information to the server’s error log, or uses it for other purposes.var_dump()andwp_die()Perform output checks (only for the development environment). Using debugging plugins such as Query Monitor is also an efficient option.

How can I publish the plugin I developed to the official WordPress plugin directory?

First of all, you need to ensure that your plugin code meets the release requirements for WordPress, including compliance with the GPL license, code quality, security, and internationalization. Next, apply for an SVN repository on WordPress.org, submit your code there, and go through the review process. The official website provides a detailed guide for submitting plugins.