替代 WordPress 的 get_site_url() [英] Alternative to WordPress's get_site_url()

查看:21
本文介绍了替代 WordPress 的 get_site_url()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取类似于 WordPress 站点 url 的值来识别某个 WordPress 安装.

I'm trying to get the value similar to the WordPress site url to identify a certain WordPress installation.

由于 get_site_url 可以改变(使用插件或直接),我想出了这个:

Since the get_site_url can get altered (with a plugin or directly) I came up with this:

untrailingslashit('http://'.$_SERVER['HTTP_HOST'].str_replace($_SERVER['DOCUMENT_ROOT'],'', ABSPATH));

这给了我很好的结果来根据它的地址识别 WordPress,但我很好奇这是否会导致某些服务器安装出现问题?

This gives me good result to identify the WordPress based on it address but I'm curios if this may cause troubles on some server installations?

推荐答案

这是我根据您提供的信息做出的回答.

Here's my answer based on the information you've provided.

创建一个函数.首先检查主页 url 是否在 wp-config 中定义为常量.如果是,就有网址.它会覆盖任何 DB 值,因此如果设置不正确,站点将无法运行.

Create a function. First check if the home url is defined as a constant in wp-config. If so there's the URL. It overrides any DB value so if not set correctly the site won't function.

如果未设置,则运行手动数据库查询以从选项表中获取 home 的值.通过手动运行而不是使用 get_option,插件无法在使用之前更改此值.

If that's not set then run a manual DB query to get the value of home from the options table. By running it manually instead of using get_option there's no way this value can be changed by a plugin before you use it.

function wpse_get_license_url() {
     global $wpdb;

     if ( defined( 'WP_HOME' ) ) {
         return WP_HOME;
     } else {
         $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", 'home' ) );
         return $row->option_value;
     } 
}

这篇关于替代 WordPress 的 get_site_url()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆