To build a WordPress plugin from scratch, create a folder in wp-content/plugins, add a PHP file with a plugin header comment, and use hooks to add your feature. Prefix everything, sanitise input, escape output, check permissions and nonces, and load scripts with wp_enqueue_script. Start small: one clear feature, tested on a staging site.
- A plugin is just a PHP file with a special comment at the top.
- Use a plugin for features that should survive a theme change.
- Security basics: sanitise input, escape output, check capabilities, verify nonces.
- Use activation and uninstall hooks to set up and clean up data.
- Follow WordPress coding standards so others can maintain your code.
When should you build a plugin instead of editing the theme?
Imagine a gym that adds a class timetable shortcode to its theme’s functions.php. A year later they switch themes and the timetable vanishes from every page. If that code had lived in a small plugin, the new theme would have kept it.
The rule of thumb: design and layout belong in the theme, features and data belong in a plugin. Custom post types, shortcodes, integrations, admin tools and business logic should all be plugins.
What does a basic plugin look like?
Create the folder wp-content/plugins/twx-notice/ and a file twx-notice.php:
<?php
/**
* Plugin Name: TWX Site Notice
* Description: Shows a small notice bar at the top of the site.
* Version: 1.0.0
* Author: Your Name
* Requires PHP: 7.4
* License: GPL-2.0-or-later
*/
if ( ! defined( 'ABSPATH' ) ) exit;
add_action( 'wp_body_open', 'twx_notice_bar' );
function twx_notice_bar() {
$text = get_option( 'twx_notice_text', 'Welcome to our site' );
echo '<div class="twx-notice">' . esc_html( $text ) . '</div>';
}
Go to Plugins in the dashboard, activate it, and the bar appears. That is a complete, working plugin.
How do you build a plugin step by step?
- Plan one feature. Write down what it does, who uses it and where it appears.
- Set up a staging site or a local environment, never a live store.
- Create the folder and main file with the header comment and the ABSPATH check.
- Add your feature with hooks. Use actions and filters as explained in WordPress hooks and filters.
- Add a settings page with the Settings API (register_setting, add_options_page) if users need options.
- Enqueue assets with wp_enqueue_style and wp_enqueue_script, only on pages that need them.
- Handle activation and uninstall with register_activation_hook and an uninstall.php file that removes your options and tables.
- Test with debug on, then with popular plugins active, then on the oldest PHP version you support.
Custom post types, plugins, theme tweaks and integrations written the WordPress way, update-safe and documented.
What security rules must every plugin follow?
| Rule | WordPress function | Stops |
|---|---|---|
| Sanitise input | sanitize_text_field, absint, sanitize_email | Bad or malicious data in your database |
| Escape output | esc_html, esc_attr, esc_url, wp_kses_post | Cross-site scripting (XSS) |
| Check permissions | current_user_can | Users doing things they should not |
| Verify nonces | wp_nonce_field, check_admin_referer | Forged requests (CSRF) |
| Safe SQL | $wpdb->prepare | SQL injection |
How should you organise a bigger plugin?
- Keep the main file short and load other files from an
includes/folder. - Use classes or namespaces to avoid name clashes.
- Split admin code from front-end code so the front end stays light.
- Make text translatable with __() and a text domain.
- Add your own hooks so the plugin can be extended later.
- Version your code with Git and write a changelog.
Frequently asked questions
Do I need to know PHP to build a WordPress plugin?
Yes, basic PHP is needed. HTML, CSS and some JavaScript help for the interface. You can start with very small plugins and learn as you go.
Plugin or functions.php: which is better?
Use a plugin for features and data that should survive a theme change. Use the child theme functions.php only for design-related tweaks.
How do I make my plugin secure?
Sanitise all input, escape all output, check user capabilities, verify nonces on forms and use $wpdb->prepare for database queries.
Can I sell my WordPress plugin?
Yes. Plugins must follow the GPL licence, but you can sell licences, support and updates, or offer a free version on wordpress.org with a paid upgrade.
How long does it take to build a custom plugin?
A small feature can take a few hours. Plugins with settings pages, custom tables and integrations take days or weeks.
Sources
Custom post types, plugins, theme tweaks and integrations written the WordPress way, update-safe and documented.
More WordPress guides
Guide, 4 min readHow to Create a Custom WordPress Theme: Beginner Guide (2026)
Guide, 4 min readHow to Create Custom Post Types in WordPress (2026 Guide)
Guide, 4 min readWordPress Hooks and Filters Explained with Examples (2026 Guide)
Guide, 4 min readHire a WordPress Developer or Learn It Yourself? Cost and Time GuideRather have an expert run your WordPress site?
Tanvir H is a Bangladesh-based WordPress and Elementor web designer with 7+ years of experience, 650+ projects and a 4.9 client rating. New websites, redesigns, fixes, speed, security and SEO, handled by one expert who explains everything in plain English and works with clients worldwide.



