- 首先,您需要在您的 WordPress 主题中编写代码来实现远程授权和限制域名。您可以在主题的
functions.php文件中添加以下代码:
function your_theme_authorization_check() {
$authorized_domains = array( 'example.com' ); // 只允许 example.com 使用主题
$current_domain = $_SERVER['HTTP_HOST'];
if ( !in_array( $current_domain, $authorized_domains ) ) {
wp_die( 'This theme can only be used on example.com.' );
}
}
add_action( 'after_setup_theme', 'your_theme_authorization_check' );
function your_theme_remote_authorization_check() {
$remote_url = 'http://example.com/remote-authorization.php'; // 远程授权验证脚本的 URL
$current_domain = $_SERVER['HTTP_HOST'];
$response = wp_remote_get( $remote_url . '?domain=' . $current_domain );
if ( is_wp_error( $response ) ) {
wp_die( 'There was an error verifying your authorization.' );
}
$authorized = wp_remote_retrieve_body( $response );
if ( $authorized != 'true' ) {
wp_die( 'This theme can only be used on authorized domains.' );
}
}
add_action( 'after_setup_theme', 'your_theme_remote_authorization_check' );
动态获取域名
function your_theme_authorization_check() {
$authorized_domains = array( 'example.com' );
$current_domain = $_SERVER['HTTP_HOST'];
if ( !in_array( $current_domain, $authorized_domains ) ) {
wp_die( 'This theme can only be used on example.com.' );
}
}
这样,$current_domain 变量将包含当前使用主题的域名,并将其与允许的域名列表进行比较。如果当前域名不在列表中,则会显示一条错误消息并阻止主题的进一步加载。
注意,$_SERVER['HTTP_HOST'] 只能获取当前访问的 URL 的主机部分,而不是整个 URL。如果您需要获取整个 URL,请使用 $_SERVER['REQUEST_URI'] 和 $_SERVER['HTTPS'] 等其他超全局变量。
完整示例
/**
* 远程授权验证
*/
function your_theme_authorization_check() {
$authorized_domains = array( 'example.com' ); // 只允许 example.com 使用主题
$current_domain = $_SERVER['HTTP_HOST'];
if ( !in_array( $current_domain, $authorized_domains ) ) {
wp_die( 'This theme can only be used on example.com.' );
}
}
add_action( 'after_setup_theme', 'your_theme_authorization_check' );
function your_theme_remote_authorization_check() {
$remote_url = 'http://example.com/remote-authorization.php'; // 远程授权验证脚本的 URL
$current_domain = $_SERVER['HTTP_HOST'];
$response = wp_remote_get( $remote_url . '?domain=' . $current_domain );
if ( is_wp_error( $response ) ) {
wp_die( 'There was an error verifying your authorization.' );
}
$authorized = wp_remote_retrieve_body( $response );
if ( $authorized != 'true' ) {
wp_die( 'This theme can only be used on authorized domains.' );
}
}
add_action( 'after_setup_theme', 'your_theme_remote_authorization_check' );
要使此示例正常工作,您需要进行以下更改:
- 将
$authorized_domains数组中的example.com更改为您希望允许使用主题的域名列表。 - 将
$remote_url变量中的http://example.com/remote-authorization.php更改为您在服务器上部署的实际远程授权验证脚本的 URL。 - 确保您在远程授权验证脚本中正确处理了传递的
domain参数,并根据需要返回true或false。
如果您的 WordPress 站点部署在一个负载均衡器或反向代理之后,$_SERVER['HTTP_HOST'] 可能无法正确检测到当前域名。这是因为负载均衡器或反向代理可能会在将请求转发到实际服务器之前修改 Host 标头。
在这种情况下,您可以考虑使用其他可用的 HTTP 标头来获取当前域名。例如,$_SERVER['X-Forwarded-Host'] 可能包含原始请求的主机部分,而不是负载均衡器或反向代理的地址。
以下是一个示例代码,演示如何使用 $_SERVER['X-Forwarded-Host'] 检测当前域名:
function your_theme_authorization_check() {
$authorized_domains = array( 'example.com' ); // 只允许 example.com 使用主题
$current_domain = $_SERVER['HTTP_HOST'];
if ( isset( $_SERVER['X-Forwarded-Host'] ) ) {
$current_domain = $_SERVER['X-Forwarded-Host'];
}
if ( !in_array( $current_domain, $authorized_domains ) ) {
wp_die( 'This theme can only be used on example.com.' );
}
}
add_action( 'after_setup_theme', 'your_theme_authorization_check' );
在这个示例中,我们首先检查 $_SERVER['HTTP_HOST'] 是否包含当前主机名,如果不包含,则使用 $_SERVER['X-Forwarded-Host'] 来获取当前域名。然后,我们像之前一样检查当前域名是否在允许的域名列表中。
请注意,$_SERVER['X-Forwarded-Host'] 只在负载均衡器或反向代理配置了正确的情况下才会可用