Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using two domain same database with different wordpress theme

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 ?

like image 236
Fatih Toprak Avatar asked May 25 '26 07:05

Fatih Toprak


1 Answers

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.

like image 118
Roscius Avatar answered May 28 '26 15:05

Roscius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!