DPS.MEDIA
WP Plugin Dev Guide — Internal
H
Hiển CEO · DPS.MEDIA

WordPress
Plugin Dev
Guide

Flow source → build → release cho team. Đủ để dev local, đóng ZIP, cài lên WordPress production.

PHP 8.x WordPress hooks API @wordpress/scripts React (Admin UI) Webpack 5 npm scripts Node.js ≥ 18 ZIP release
Cấu trúc thư mục
my-plugin/ — project root
Source (dev)
my-plugin.php ← file plugin chính
includes/ ← PHP runtime
class-core.php
class-admin.php
admin/
src/ ← viết code ở đây
index.js ← entry point
components/
dist/ ← output build
index.js
index.asset.php
index.css
languages/ ← i18n
node_modules/ ← .gitignore
releases/ ← .gitignore
Config files
package.json
webpack.config.js ← nếu cần custom
.gitignore
BUILD.md
readme.txt
uninstall.php
// ✓ Đúng — enqueue từ dist
plugins_url(‘admin/dist/index.js’)
// ✗ Sai — không enqueue src
plugins_url(‘admin/src/index.js’)
Workflows
Local Development

Dev local

Chạy watch mode, sửa JS/React → build tự động, F5 WP là thấy ngay.

01
Cài dependencies
Chỉ cần lần đầu. Yêu cầu Node.js ≥ 18.
npm install
02
Bật watch mode
Webpack watch: save file trong admin/src/ → tự build ra admin/dist/. Cần F5 tay trong WP.
npm run start
03
Test trên WP local
Symlink vào wp-content/plugins/ hoặc dùng Local by Flywheel.
Build & Release

Đóng ZIP, cài WP

Build production, đóng gói ZIP sạch — không cần npm trên server.

01
Build production
Webpack build với minify + tree-shaking.
npm run build
02
Đóng gói ZIP
Script copy đúng file, loại node_modules và admin/src.
npm run package
# → releases/plugin-v1.0.0.zip
03
Verify SHA256
sha256sum releases/plugin-v1.0.0.zip
✓
Upload lên WordPress
Admin → Plugins → Add New → Upload. Activate. Không cần npm trên server.
npm scripts — cheat sheet
npm run start
Watch mode — dev hằng ngày
webpack –watch –mode development
npm run build
Production build — trước khi release
webpack –mode production + minify
npm run package
Đóng ZIP sạch vào releases/
exclude src/ node_modules/ .git/
npm run lint:js
Lint JS theo chuẩn WP
wp-scripts lint-js
npm run format
Format code tự động
wp-scripts format (prettier)
npm run release
Lint → build → package
check + build + package in 1 cmd
ZIP release — trong và ngoài
✓ Có trong ZIP
my-plugin.phpfile plugin chính
includes/PHP runtime toàn bộ
admin/dist/JS/CSS đã build sẵn
languages/i18n nếu có
readme.txtWP.org format
uninstall.phpcleanup khi xoá plugin
✗ Không có trong ZIP
node_modules/deps dev only
admin/src/source, không chạy
.git/metadata repo
package.json / webpack.config.jsconfig build
*.test.js / debug-*.phpfile test nội bộ
.env / .DS_Storefile local machine
Quy tắc quan trọng
Làm đúng
Enqueue từ admin/dist/
Đọc index.asset.php lấy deps + version
Dùng wp_localize_script() pass data PHP → JS
Chạy npm run build trước npm run package
Conditional load — chỉ enqueue đúng page cần
Prefix tất cả: dps_, DPS_
Đừng làm
Enqueue từ admin/src/
Hardcode version hash trong PHP
Để node_modules/ trong ZIP
npm install trên production server
Chạy DB query nặng ở init hook
echo $_POST['x'] không sanitize/escape
Checklist production-ready
Build & Release
✓
npm run build không lỗi → admin/dist/
✓
ZIP cài được, Activate không lỗi — không cần npm server
✓
ZIP không chứa node_modules/ hay src/
✓
Báo lại: danh sách file + ZIP path + SHA256
Security
✓
Mọi file PHP có if (!defined('ABSPATH')) exit;
✓
Mọi AJAX/form: nonce verify + current_user_can()
✓
Input sanitized, output escaped
Performance
✓
Asset load conditional — không load toàn site
✓
Script: in_footer: true + strategy: defer
✓
Không query DB nặng ở init hook
Standards
✓
Mọi function/class/option có prefix dps_
✓
Plugin header: Requires at least, Requires PHP
✓
Tất cả string dùng i18n, uninstall.php cleanup sạch
✓
BUILD.md đầy đủ: install, build, package, test

DPS.MEDIA

Ý kiến bạn đọc (9)

DPS SECURITY

Đăng nhập để bình luận

Đăng nhập nhanh 1-chạm bằng Google để chia sẻ ý kiến của bạn về bài viết.

duc_it
duc_it 28/08/2026

Bên mình dev plugin khá nhỏ, vậy có nhất thiết phải tách src và dist như guide không? Em thấy thêm bước build cũng hơi mất thời gian.

Mê Công Nghệ
Mê Công Nghệ 28/08/2026

Nếu plugin chỉ có PHP thì không cần cầu kỳ vậy. Nhưng đã có React/Admin UI thì tách src và dist khá đáng, vì server production chỉ cần bộ đã build, không phải cài Node hay npm.

ktshoangdung
ktshoangdung Chuyên gia 28/08/2026

Chuẩn. Cái lợi lớn là release sạch và ít phụ thuộc môi trường server. Bên mình còn đưa lint, build, package vào một lệnh để giảm lỗi lúc đóng ZIP.

tranthidung1604
tranthidung1604 28/08/2026

Đoạn đóng ZIP em quan tâm nhất: nếu đã build production rồi thì ZIP nên chứa những gì để cài lên WP không bị lỗi thiếu file?

L. Buil
L. Buil 28/08/2026

Tối thiểu là file plugin chính, toàn bộ PHP runtime trong includes, thư mục dist đã build và các file i18n/readme/uninstall nếu plugin có dùng. Không nên nhét node_modules, source JS hay thư mục .git vào bản release.

hieunt_88
hieunt_88 Chuyên gia 28/08/2026

Nói thật bước verify SHA256 cũng đáng giữ, nhất là team có nhiều người đóng gói. Nó giúp đối chiếu đúng file ZIP trước khi đưa lên production.

Hai Lúa
Hai Lúa 28/08/2026

Công nhận flow source → build → release rõ ràng hơn hẳn. Dev local chỉ cần watch rồi F5 WP là khá ổn áp.

quangvinh92
quangvinh92 28/08/2026

Cho em hỏi Node.js tối thiểu 18 là yêu cầu lúc dev thôi đúng không? Production chỉ upload ZIP và activate, không cần npm nữa?

Kỹ Sư Quèn
Kỹ Sư Quèn 28/08/2026

Em thích nhất phần package tự động exclude src, node_modules và .git. Đóng plugin thủ công mấy lần rất dễ quên một thư mục rồi ZIP phình to.

Sẵn sàng bứt phá doanh thu cùng DPS.MEDIA?

Đồng hành cùng hơn 5.400+ SMEs tối ưu hóa chi phí Marketing, nâng cao vị thế thương hiệu và tự động hóa tăng trưởng bằng công nghệ AI 2026.