-
목차
🔧 왜 플러그인을 구조화해야 할까?
워드프레스 플러그인은 전통적으로 functions.php 수준의 단일 파일로도 구현이 가능하지만,
✅ 기능이 많아질수록 → 파일 복잡도 증가
✅ 다른 플러그인과 충돌 → 함수 중복
✅ 유지보수 어려움 → 개발자 추가 시 리스크👉 따라서 객체지향 설계(OOP) + 네임스페이스 활용 + 모듈 분리가 필요합니다.
✅ OOP 플러그인 아키텍처 기본 구조
perl복사편집my-plugin/ ├── includes/ │ ├── Admin/ │ │ └── SettingsPage.php │ ├── Frontend/ │ │ └── ShortcodeHandler.php │ └── Core/ │ └── Loader.php ├── my-plugin.php ├── autoload.php └── README.md
🧩 핵심 개념 1: 네임스페이스와 클래스 사용
예: includes/Admin/SettingsPage.php
php복사편집namespace MyPlugin\Admin; class SettingsPage { public function register() { add_action('admin_menu', [$this, 'add_menu']); } public function add_menu() { add_menu_page('My Plugin', 'My Plugin', 'manage_options', 'my_plugin', [$this, 'render']); } public function render() { echo '<h1>My Plugin Settings</h1>'; } }📌 클래스 이름 충돌 방지를 위해 namespace 필수
🧩 핵심 개념 2: 메인 플러그인 파일에서 초기화
my-plugin.php
php복사편집/** * Plugin Name: My Structured Plugin */ require_once plugin_dir_path(__FILE__) . 'includes/Core/Loader.php'; use MyPlugin\Core\Loader; $loader = new Loader(); $loader->init();
🧩 핵심 개념 3: 자동 로딩 구현 (autoload.php 또는 Composer)
php복사편집spl_autoload_register(function ($class) { $prefix = 'MyPlugin\\'; $base_dir = __DIR__ . '/includes/'; $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; } });📌 PSR-4 방식 간단 구현 (또는 composer.json으로 관리 가능)
⚙ 핵심 클래스: Loader
includes/Core/Loader.php
php복사편집namespace MyPlugin\Core; use MyPlugin\Admin\SettingsPage; use MyPlugin\Frontend\ShortcodeHandler; class Loader { public function init() { if (is_admin()) { $admin = new SettingsPage(); $admin->register(); } else { $frontend = new ShortcodeHandler(); $frontend->register(); } } }
💡 고급 설계 팁
설계 전략설명의존성 주입(DI) 클래스 내부에서 직접 인스턴스 생성 대신 외부 주입 싱글톤 패턴 주요 인스턴스 1회 생성 → 반복 사용 인터페이스 도입 설정 저장/렌더링 등 공통 인터페이스 정의 훅 관리 전용 클래스 add_action, add_filter 전용 로더 분리 MVC 구조 확장 View 분리 시 views/ 디렉토리 도입
✅ 클래스 기반 플러그인 설계 체크리스트
항목완료 여부네임스페이스로 클래스 분리 ✅ / ❌ 파일명과 클래스명 일치 ✅ / ❌ 오토로딩 구현 또는 Composer 사용 ✅ / ❌ 관리자/프론트엔드 기능 분리 ✅ / ❌ 의존성 최소화 및 재사용 가능성 고려 ✅ / ❌ '워드프레스' 카테고리의 다른 글
🛒 WooCommerce의 성능과 전환율은 왜 중요한가? (3) 2025.05.13 🌟 블록 테마(Block Theme)란? (2) 2025.05.08 🚀 워드프레스 백엔드 성능이 중요한 이유 (1) 2025.05.07 🔍 기본 워드프레스 검색의 한계 (0) 2025.05.06 🤖 왜 워드프레스에 AI를 도입해야 할까? (3) 2025.05.05 🧠 워드프레스 데이터 저장 방식의 핵심 (2) 2025.05.04 🧠 왜 마이크로서비스가 필요한가? (2) 2025.05.03 🎯 왜 워드프레스를 버전 관리해야 할까? (1) 2025.05.02