好的,我带你去旅行吧。
您的集中式开发环境听起来非常独特,但我认为我们可以使用它。我有一个类似的系统设置:我们有一个“staging”服务器,带有一个公共域/ ip供客户端查看,每个开发人员都有一个完整的副本,通过git在他们的本地机器上使用本地Apache服务器。我们将本地开发站点指向中央登台服务器数据库,以便内容/设置保持一致。我们使用自定义本地域访问我们的本地开发站点
http://example.local
,临时站点是
http://example.staging.com
,现场网站将是
http://example.com
。
另外,我们有
wp-config.php
在
.gitignore
,我们每个人都可以在我们的本地副本上进行自定义。通常我们将staging和live版本保存为不同命名的文件
wp-config-live.php
和符号链接到
wp-config.php
在实时服务器等,以便我们也可以在我们的私人git仓库中保留配置设置。
现在,我们想要处理多站点的本地副本
example.local
并通过子文件夹访问其网站(
example.local/site2/
)或我们在vhost设置中别名的其他自定义域(
example2.local
)在每台计算机上。此外,暂存站点应该没有任何东西工作
git pull
在服务器上。最重要的是,现场网站应该
也
工作简单
git pull
没有任何数据库编辑。我们如何实现这一目标?
首先,我们的
wp-config
所有live / staging / dev环境中应包含标准多站点设置:
define( ‘WP_ALLOW_MULTISITE’, true );
define( ‘MULTISITE’, true );
define( ‘SUBDOMAIN_INSTALL’, false );
define( ‘DOMAIN_CURRENT_SITE’, ‘example.com’ );
define( ‘PATH_CURRENT_SITE’, ‘/‘ );
define( ‘SITE_ID_CURRENT_SITE’, 1 );
define( ‘BLOG_ID_CURRENT_SITE’, 1 );
</code>
请注意
DOMAIN_CURRENT_SITE
是个
生活
域!为了使整个系统工作,我们数据库中定义的域应该都是实时域,我们将使用以下内容处理每个staging / dev站点,
wp-config
(仅限dev / staging配置,不在实时配置中):
/ Set this to your local tld setup */
define( ‘WP_HOME’, ‘http://example.local‘);
define( ‘WP_SITEURL’, ‘http://example.local‘);
/ Include wp-content/sunrise.php /
define( ‘SUNRISE’, true );
/** This should be the TLD in the database /
define( ‘WP_PROD_TLD’, ‘.com’ );
/* This should be the tld of your local copy /
define( ‘WP_DEV_TLD’, ‘.local’);
</code>
该
WP_PROD_TLD
和
WP_DEV_TLD
是我们自己的自定义常量,我们将用它来检查我们是否需要更改域名(我意识到您在设置中也使用了子域名,但我想在尝试适合您之前描述一种稍微“标准”的方法)具体情况)。该
SUNRISE
常量内置于WP多站点,它只是说包含
/wp-content/sunrise.php
如果它存在这是我们的
sunrise.php
文件:
<?php
/**
- File sunrise.php
* - This allows us to copy the production multisite database to staging/dev and still use
- it directly without altering domains
*/
/**
Filter /wp-includes/ms-load.php get_site_by_path to find production domains
**/
function dev_get_site_by_path($_site, $_domain, $_path, $_segments, $_paths) {
global $wpdb, $path;
// Get our actual domain in the database (should be set to production domain)
// The domain coming in should be the request domain
$domain = str_replace( WP_DEV_TLD, WP_PROD_TLD, $_domain);
// Search for a site matching the domain and first path segment
$site = $wpdb->get_row( $wpdb->prepare( “SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s”, $domain, $_paths[0] ) );
$current_path = $_paths[0];
if ($site === null) {
// Specifically for the main blog - if a site is not found then load the main blog
$site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s and path = %s", $domain, '/' ) );
$current_path = '/';
}
// Set path to match the first segment
$path = $current_path;
return $site;
}
add_filter(‘pre_get_site_by_path’, ‘dev_get_site_by_path’, 1, 5);
add_filter(‘pre_get_network_by_path’, ‘dev_get_site_by_path’, 1, 5);
/**
- Filter the site_url and home options for each site, and
- filter /wp-includes/link-template.php::network_site_url()
- and /wp-includes/link-template.php::network_home_url()
so that our network site link is correct in the admin menu
*/
function dev_network_url( $_url = ‘’ ) {
return str_replace( WP_PROD_TLD, WP_DEV_TLD, $_url );
}
add_filter( ‘network_site_url’, ‘dev_network_url’ );
add_filter( ‘network_home_url’, ‘dev_network_url’ );
add_filter( ‘option_siteurl’, ‘dev_network_url’ );
add_filter( ‘option_home’, ‘dev_network_url’ );
请注意
sunrise.php
文件是
的
仅通过DEV-STAGING上的WP-CONFIG包含
</强>
,永远不会在实时服务器上!这是过滤请求以获取网络“按路径的站点”,这意味着它查看从客户端请求的域和路径,并匹配域中的域和路径
wp_blogs
表。我们正在检查域名中的实时TLD(在这种情况下,
.com
并将其替换为当前环境的TLD(我们的本地副本是
.local
,登台服务器是
.staging.com
)。它也过滤了
WP_HOME
和
WP_SITEURL
底部的每个站点的选项。
而且,就是这样!正在为您的本地/临时站点设置正在翻译数据库中的实时域名!只要您使用子文件夹或在本地虚拟主机设置中设置别名,这应该有效。
现在,对于您使用子域的情况,您可能必须对域进行更彻底的过滤,而不是仅替换TLD。也许你满足于数据库中的“规范”域名
multisite.dev
然后在你的
sunrise.php
文件只是附加本地用户的子域而不是替换TLD,然后在准备好迁移后在数据库中替换实时域。
感谢“laubsterboy”让我开始寻求这个解决方案:
https://www.laubsterboy.com/blog/2014/08/running-development-copy-wordpress-multisite-update/