- 1. Vì sao nên tách module?
- 2. Cấu trúc thư mục đề xuất
- 3. Autoload đơn giản trong functions.php
- 4. Ví dụ module cụ thể
- 5. Với plugin hoặc dự án PHP riêng?
- 6. Ưu tiên module theo nhóm chức năng
- Kết luận
- 7. Cải tiến autoload cho module theo tên file
- 8. Tạo bộ khung module dùng class
- 9. Viết module dạng reusable
- 10. Dùng namespace để tránh xung đột
- 11. Đề xuất starter kit
- Kết luận mở rộng
1. Vì sao nên tách module?
- Giảm kích thước file
functions.phphoặc index chính. - Dễ tìm, dễ sửa từng phần chức năng.
- Cho phép nhiều người làm việc song song ở các module riêng biệt.
- Hạn chế xung đột khi mở rộng hệ thống.
2. Cấu trúc thư mục đề xuất
my-theme/
├── functions.php
├── modules/
│ ├── admin/
│ │ └── settings-page.php
│ ├── security/
│ │ ├── disable-xmlrpc.php
│ │ └── headers.php
│ ├── performance/
│ │ └── cache-headers.php
│ └── helpers/
│ └── general.php
3. Autoload đơn giản trong functions.php
$modules = glob(__DIR__ . '/modules/**/*.php');
foreach ($modules as $module) {
require_once $module;
}
Lưu ý: Bạn cần PHP 7+ và glob() được bật trên host. Nếu không hỗ trợ dấu ** (đệ quy), bạn có thể viết thủ công bằng RecursiveIteratorIterator.
4. Ví dụ module cụ thể
modules/security/disable-xmlrpc.php
add_filter('xmlrpc_enabled', '__return_false');
modules/helpers/general.php
function theme_asset($path) {
return get_template_directory_uri() . '/assets/' . ltrim($path, '/');
}
5. Với plugin hoặc dự án PHP riêng?
Bạn vẫn có thể tách module nếu dùng plugin hoặc code PHP thuần:
// Autoload dạng PSR-4 (giả lập thủ công)
spl_autoload_register(function($class) {
$prefix = 'MyApp\\';
$base_dir = __DIR__ . '/modules/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) return;
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) require $file;
});
6. Ưu tiên module theo nhóm chức năng
Thay vì gộp chung tất cả hook hoặc function, hãy chia theo mục đích chức năng:
security/: xử lý headers, chặn bot, tắt XML-RPC…performance/: preload, cache headers, lazy load…admin/: thêm menu settings, custom admin style…
Kết luận
Cấu trúc module giúp dự án PHP hoặc WordPress của bạn gọn gàng, dễ đọc và dễ bảo trì. Việc tách các đoạn mã nhỏ ra nhiều file rõ ràng theo mục đích cũng giúp bạn debug nhanh và mở rộng hệ thống trong tương lai một cách hiệu quả hơn.
Mẹo: Với những project lớn hơn, bạn có thể dùng Composer để quản lý autoload module và tích hợp thêm các chuẩn PSR.
7. Cải tiến autoload cho module theo tên file
Nếu bạn muốn tự động nạp module theo tên file rõ ràng, bạn có thể dùng cách này thay vì glob() toàn bộ:
// Trong functions.php
$modules = [
'helpers/general.php',
'security/disable-xmlrpc.php',
'security/headers.php',
'performance/cache-headers.php',
'admin/settings-page.php',
];
foreach ($modules as $file) {
$path = __DIR__ . '/modules/' . $file;
if (file_exists($path)) {
require_once $path;
}
}
Với cách này, bạn dễ quản lý thứ tự nạp file nếu cần phụ thuộc lẫn nhau.
8. Tạo bộ khung module dùng class
Thay vì mỗi file là tập lệnh procedural, bạn có thể dùng lớp (class) để rõ ràng hơn và dễ mở rộng.
// modules/security/Disable_XMLRPC.php
namespace Modules\Security;
class Disable_XMLRPC {
public static function init() {
add_filter('xmlrpc_enabled', '__return_false');
}
}
Sau đó gọi ở functions.php:
require_once __DIR__ . '/modules/security/Disable_XMLRPC.php';
\Modules\Security\Disable_XMLRPC::init();
9. Viết module dạng reusable
Bạn có thể biến một số module thành các hàm tiện ích có thể tái sử dụng ở nhiều nơi khác nhau.
modules/helpers/debug.php
function debug_log($data) {
if (is_array($data) || is_object($data)) {
error_log(print_r($data, true));
} else {
error_log($data);
}
}
Rất tiện cho các thao tác log trong dev.
10. Dùng namespace để tránh xung đột
Khi dự án lớn, hãy sử dụng namespace để module không bị trùng function hoặc class.
namespace MyTheme\Performance;
function disable_emojis() {
remove_action('wp_head', 'print_emoji_detection_script', 7);
}
Lúc gọi:
\MyTheme\Performance\disable_emojis();
11. Đề xuất starter kit
Bạn có thể bắt đầu với một bộ khung gồm:
modules/
├── helpers/
│ └── general.php
├── security/
│ ├── disable-xmlrpc.php
│ └── headers.php
├── admin/
│ └── settings-page.php
├── performance/
│ └── cache-control.php
├── seo/
│ └── disable-yoast-comments.php
Mẹo: Bạn có thể gộp nhiều module lại thành plugin riêng để dễ tái sử dụng cho nhiều site.
Kết luận mở rộng
Việc tổ chức mã PHP theo module không chỉ là “gọn”, mà là điều bắt buộc nếu bạn muốn làm dự án dài hơi. Nó giúp bạn test từng phần riêng biệt, tránh phụ thuộc toàn cục, và dễ dàng chuyển từ procedural sang hướng đối tượng nếu cần.
Bình luận