Để thực hiện các bước trên thông qua một plugin tùy chỉnh, bạn có thể tạo một plugin mới với thư mục và file functions.php
riêng. Dưới đây là các bước cụ thể:
wp-content/plugins/
trong cài đặt WordPress của bạn.custom-subdomain-theme
.custom-subdomain-theme
, tạo một file PHP mới và đặt tên là functions.php
.functions.php
Mở file functions.php
vừa tạo và thêm mã sau đây vào:
<?php /* Plugin Name: Custom Subdomain Theme Selector Description: Thay đổi theme dựa trên subdomain. Version: 1.0 Author: Tên của bạn */ add_filter('template', 'change_theme_based_on_subdomain'); add_filter('stylesheet', 'change_theme_based_on_subdomain'); function change_theme_based_on_subdomain($theme) { $domain = $_SERVER['HTTP_HOST']; if ($domain === 'theme1.web.com') { return 'theme1'; // Thư mục theme cho theme1 } elseif ($domain === 'theme2.web.com') { return 'theme2'; // Thư mục theme cho theme2 } return $theme; }
Custom Subdomain Theme Selector
và nhấn Activate để kích hoạt.Plugin này sẽ tự động thay đổi theme dựa trên subdomain người dùng truy cập. Ví dụ, nếu người dùng vào theme1.web.com
, WordPress sẽ sử dụng theme có tên thư mục là theme1
; nếu vào theme2.web.com
, nó sẽ sử dụng theme có tên thư mục là theme2
.