实现这一点的一种方法是,通过使用sunrise.php
请进。Sunrise覆盖中的默认设置wp-includes/ms-settings.php
. 在这里,一组全局变量被设置为$current_blog
或$current_site
.
要激活此下拉菜单,必须定义常量SUNRISE
在wp配置中。php:
define( \'SUNRISE\', TRUE );
现在,WordPress查找
sunrise.php
在您的
WP_CONTENT_DIR
目录(默认情况下为
/wp-content
).
在这日出的时候。php您必须至少做以下事情:
解析请求以获取域和路径设置全局$current_site
使用函数get_current_site()
在中查找您的博客wp_blogs
使用域和路径的数据库表使用此数据,设置全局$current_blog
这里有一个我是如何玩这个的例子。这并不完美,但可能会让您了解这可能是什么样子:
(我使用名称空间,所以请注意至少使用PHP 5.3或将名称空间引用分条。)
所示的示例并没有像您希望的那样将路径段限制为两个级别,而是将完整的路径与数据库相匹配,这样即使是一个或两个以上的级别也可以作为自己博客的基础。
namespace dna\\Blog_Mapping;
sunrise();
/**
* init the globals $current_blog and $current_site
*
* @global $current_blog
* @global $current_site
* @global $wpdb
* @global $path
* @global $cookie_domain
* @return void
*/
function sunrise() {
if ( defined( \'WP_INSTALLING\' ) )
return; # don\'t touch the installing process
if ( ! \\is_subdomain_install() )
return;
/**
* strip port numbers in the host
*/
$domain = addslashes( $_SERVER[\'HTTP_HOST\'] );
if ( false !== strpos( $domain, \':\' ) ) {
if ( substr( $domain, -3 ) == \':80\' ) {
$domain = substr( $domain, 0, -3 );
$_SERVER[\'HTTP_HOST\'] = substr( $_SERVER[\'HTTP_HOST\'], 0, -3 );
} elseif ( substr( $domain, -4 ) == \':443\' ) {
$domain = substr( $domain, 0, -4 );
$_SERVER[\'HTTP_HOST\'] = substr( $_SERVER[\'HTTP_HOST\'], 0, -4 );
} else {
\\wp_load_translations_early();
\\wp_die( __( \'Multisite only works without the port number in the URL.\' ) );
}
}
$current_site = \\get_current_site();
# setup the sitename to the current_site object
$current_site = $current_site->site_name;
# find the blog by domain and path
$uri = \\parse_url( $_SERVER[ \'REQUEST_URI\' ] );
$path = \\preg_replace( \'|([a-z0-9-]+.php.*)|\', \'\', $uri[ \'path\' ] );
$path = \\str_replace ( \'/wp-admin/\', \'/\', $path );
$current_blog = get_blog_details( $domain, $path );
# at this point, we are obviously at the root blog
# using the core function to get blog by root blog id
if ( ! $current_blog ) {
$current_blog = \\get_blog_details( $current_site->blog_id );
}
if ( empty( $current_blog->site_id ) ) {
$current_blog->site_id = ( isset( $current_site->id ) )
? $current_site->id
: 1;
}
$blogname = substr( $domain, 0, strpos( $domain, \'.\' ) );
$blogname .= $current_blog->path;
# set globals like in wp-includes/ms-settings.php
$GLOBALS[ \'blog_id\' ] = $current_blog->blog_id;
$GLOBALS[ \'public\' ] = $current_blog->public;
$GLOBALS[ \'current_site\' ] = $current_site;
$GLOBALS[ \'current_blog\' ] = $current_blog;
$GLOBALS[ \'path\' ] = $current_blog->path;
$GLOBALS[ \'blogname\' ] = $blogname;
}
/**
* get the blog details by domain and
* matching path segments
*
* @global $wpdb
* @param string $domain
* @param string $path
* @return \\stdClass|FALSE
*/
function get_blog_details( $domain, $path ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT
*
FROM
{$wpdb->blogs}
WHERE
domain = %s
AND
1 = INSTR( %s, path )
ORDER BY
CHAR_LENGTH( path ) DESC
LIMIT 1",
$domain,
$path
);
$current_blog = $wpdb->get_row( $query );
if ( empty( $current_blog ) )
return FALSE;
return $current_blog;
}