using two domain same database with different word

2020-02-26 13:58发布

I want to use, same database, (content / users / comments / meta's / categories etc.) for another wordpress install in my sub directory.

I actually want to create mobile version of my site. But i dont want to use, any mobile detect script ory css3 media queries. Just want to create my new theme (for mobile version)

For example;

the main domain has also sub domain like ;

maindomain.com // root
mobile.maindomain.com // sub directory

How this could be possible ?

2条回答
放荡不羁爱自由
2楼-- · 2020-02-26 14:13

You cant do that directly, as WP stores its url in database. AFAIK there is a way to "link" tables in sql, so it might be the way - link everything beside wp_options for example, but still it is a tricky way.

查看更多
SAY GOODBYE
3楼-- · 2020-02-26 14:32

One of the nice things about WordPress is the great number of hooks in the code allowing you to extend or override core functionality.

One way to approach this problem would be to set an Apache environment variable in your vhost file for each site that could be used in the WordPress bootstrap process to over-ride the theme and base URL setup.

e.g. in Apache vhost add:

SetEnv WP_CONTEXT main

and

SetEnv WP_CONTEXT mobile

(or equivalent if you're using a different webserver).

In wp-config.php:

switch ($_SERVER['WP_CONTEXT']) {
    case 'main':
        define('WP_HOME','http://maindomain.com');
        define('WP_SITEURL','http://maindomain.com');
    break;

    case 'mobile':
        define('WP_HOME','http://mobile.maindomain.com');
       define('WP_SITEURL','http://mobile.maindomain.com');
    break;
}

This will set the base URLs based on the environment variable.

Then in plugin add the following filters:

add_filter('template', 'change_theme');
add_filter('option_template', 'change_theme');
add_filter('option_stylesheet', 'change_theme');

function change_theme() 
{
    switch ($_SERVER['WP_CONTEXT']) {
        case 'main':
            return 'main';
        break;

        case 'mobile':
           return 'mobile';
        break;
}

This needs to be in a plugin so that it's loaded before the normal theme loading process (functions.php is part of the theme and hence too late). These filters will intercept and over-ride the theme settings from the database.

查看更多
登录 后发表回答