首页 文章

将Wordpress siteurl和home设置从数据库移动到wp-config.php

提问于
浏览
0

有没有办法可以移动我的Wordpress域配置设置:

  • siteurl

从数据库wp-options表到wp-config.php?

我希望能够将我的实时数据库与我的本地主机同步以用于开发目的,但每次都必须在数据库中更改这些设置是令人讨厌的 .

我正在使用git,并且在我的存储库中有一个wp-config.production.php和wp-config.testing.php,它在部署时与Capistrano符号链接,所以理想情况下我想分别将这些域设置添加到这些文件中 .

1 回答

  • 0

    我想出了以下解决方案,似乎工作正常:

    将以下内容添加到wp-config.php

    /** Domain settings (No trailing slash!)*/
    define ('SITEURL', 'http://domain.dev');
    define ('HOME', 'http://domain.dev');
    

    然后在function.php中添加:

    <?php
    
    update_domain_settings();
    
    /**
     * update_domain_settings
     */
    function update_domain_settings()
    {
        $domain_updated = false;
    
        if(get_option('siteurl') != SITEURL) {
            update_option('siteurl', SITEURL);
            $domain_updated = true;
        }
    
        if(get_option('home') != HOME) {
            update_option('home', HOME);
            $domain_updated = true;
        }
    
        if($domain_updated === true) {
            header("Location: " . home_url());
            exit;
        }
    }
    

    在SITEURL和HOME中省略尾部斜杠很重要,否则你最终会遇到递归重定向问题 .

相关问题