Two wordpress sites using diiferent themes on the same database - php

I want to run 2 wordpress sites, one.site.com and two.site.com from the same database. Everything remains the same except for the theme.
one.site.com - Existing site
two.site.com - Should use one.site.com's database except for the
theme
Is there any way this can be done ?
Since theme details are stored in wp_options table is it possible for two.site.com to use it to display a different theme ? Say duplicating that table and making two.site.com use it ?
I appreciate any help.
EDIT:
Both the sites do not have any plugins.

The solution below did the job for me,
Install 2 WordPress sites on a single database.
Create new table in your database. Call it wp_options2 and copy everything from wp_options into this new table
In second install go to wp-config.php, and before if (!defined('ABSPATH')) add define( 'M7_OPTIONS_TABLE', 'wp_options2');
In second install go to wp-includes/wp-db.php on line 1009 and add code:
if (isset( $tables['options'] ) && defined('M7_OPTIONS_TABLE')) $tables['options'] = M7_OPTIONS_TABLE;
These codes should be added in public function tables function, before if (isset( $tables['users']) && defined('CUSTOM_USER_TABLE')))
I found this solution here, https://wordpress.stackexchange.com/questions/84313/how-to-run-two-wordpress-blogs-with-different-themes-and-with-single-database-an#answer-175494
Since we use the same database, links will be same on both the sites. I had a lot of images linked and I removed them using
.single a[href$=".jpg"] {
pointer-events: none;
cursor: default;
}

You can run two sites from a single database but not from the same set of database tables as the stored data includes the site's domain name.
There are two values in the options table: siteurl and home which are used. Using the same options table won't work, even if you update options forcefully for each php run.
So you would need to use two databases.
EDIT:
My advice is to replicate base and run both sites on different databases.

This could probably be accomplished by using wildcard subdomains (https://codex.wordpress.org/Configuring_Wildcard_Subdomains).
It may be a little hacky, but you'd have to test the condition of your subdomain (maybe in wp-config.php?) and set the theme in the database (http://www.inmotionhosting.com/support/edu/wordpress/change-theme-in-db).
The only problem I could see with this is that your functions.php can change WP functionality, so a Parent theme with two Child Themes is probably a good idea(https://codex.wordpress.org/Child_Themes).

There is a lot of jiggery hackery regarding this question, but I think there's a fairly simple solution. First, ensure that both of your 2 sites have these hardcoded in wp-config.php:
define('WP_HOME', 'https://example.com');
define('WP_SITEURL', 'https://example.com');
These will help each site "override" whatever the database setting is for the URLs.
Next, decide which site is the MASTER site where you are going to be publishing new blog posts, making edits to site data, and such. On that site, activate the theme you want to use.
Now, go to your SLAVE site (I'm using these terms loosely) and delete that theme via SFTP, and upload whichever other theme you want to use on that site. Then, hardcode it like this:
define('WP_DEFAULT_THEME', 'twentytwenty');
In this way, the SLAVE site tries to find the correct theme from wp_options that is activated on the MASTER site but you deleted it already, so it reverts to using the default theme as defined in wp-config.php
Going forward, do not mess with site settings/plugins/etc on the SLAVE site, and make any changes to the design of your SLAVE site using the theme files only (like a static theme). Obviously if you are using a bloated theme and page builder type of situation that need MySQL, this isn't going to work...
Note: you may need to visit /wp-admin/themes.php on your SLAVE site at least once, to "force" WordPress to load the hardcoded theme.
If anyone is interested, SlickStack supports this approach.

Duplicate of https://wordpress.stackexchange.com/q/65560/wp-multisite-load-content-from-site-x-on-site-y
One might argue that using WP multisite-network functionality and adding a couple of documented API calls to ones theme could be a cleaner solution than manually modifying the database schema and messing with how WP access it.

Related

Wordpress site - using custom PHP and database

I have finally finished coding a project with an HTML form for file upload, and a large PHP file that submits a 200 field CSV into my database. It first uploads to a staging table and then splits the data into smaller tables.
This all works perfectly but I'm having problems with the next step: This is needed to run on a wordpress site that already exists. I have a database just for this project with tables for users, clients, staging, meters, etc. I need all of these tables in their own database because these users are separate from the WP users.
I found a plugin that allows you to insert PHP into pages, but I need to know how to work with a custom database in WP, I've only ever worked with the actual database. Any help is greatly appreciated.
Include wordpress db file in your script 'wp-admin/includes/upgrade.php' and then call
global $wpdb;
$wpdb->query($your_query_string);
This way you can use wordpress database. But go through wordpress once so you will be aware of tables. Because wordpress save specific data to specific tables, say - any informartion about user to user_meta table.
If you are trying to insert new users then you may need to enter data into user table.
Another method is there.
You can open your wordpress theme's function.php then you can write scripts to enter your data or what ever fuctionality you are looking for. And I think that is a better way to work with wordpress.
If you want to use php, try this tutorial: https://www.w3schools.com/php/php_ref_mysqli.asp
If you are developing wordpress plugins check out the wordpress codex: https://codex.wordpress.org/Creating_Tables_with_Plugins
This is going to take a bit of research on your part. Good luck!

Drupal site with same database for two sites - I need better solution

I am now working on a drupal project, the client needs same admin side and same database to save the content, But he needs visualize things separately. That is two sites running same code base. These two are sharing same contents. Such as news,reports etc.
What is the best way to implement this, I read some multisite doc, but its based on two admin side and different database.
Thanks in advance.
You should look into the Domain Access module. It will allow you to use a single database for a multisite setup (sharing all users and content). One of the sub modules within it Domain Theme allows you to set the active theme for each domain (making each site look different)

Ticking a checkbox with PHP to put in Wordpress function.php

I recently discovered a method of changing Wordpress preferences if you do something silly that makes the wp-admin not load. I found that inserting things into the function.php file found in the theme and using the update_option command allowed me to change the settings i needed to get be working again.
This got me thinking, i have the need to install the same theme with the same settings. This is a pain as i have to define all the settings each time. Instead i am trying to make a functions.php file that will set all the settings to how i like them.
I am able to update settings that require you to type a string into a field
//this will change the siteurl setting to www.mysite.com
update_option ('siteurl','www.mysite.com');
My issue is that i need to be able to do the same thing for checkboxes, drop down menus and for radio buttons. After a long night or searching the internet for examples i had given up until i found this site.
Any help would be appreciated, i have minimal programming experience but i do understand the concepts
in theory update_option('checkboxname',true); or update_option('checkboxname','on');
http://codex.wordpress.org/Function_Reference/update_option

Where should I store global data for my multi-site WordPress plugin?

I wrote a plugin for WordPress which has a few user-configurable settings that are stored using WordPress's suggested method. I know they are saved in the wp_options table, but that is abstracted by the WordPress options API.
Now I'm trying to add a "global override" of the settings that can be configured in the Network Admin section of a multi-site installation. I found the appropriate hooks to design my settings page, however I can't find any info about where to save the data.
If I save it using the normal options API, then the settings get saved individually for each site. I'm looking for a place to save them globally for all sites, so the plugin can first look to see if the settings have been globally overridden by the server admin.
I can just write some code to write directly to the wp_options table of one of the sites (for example site #1) or even create my own table. I know how to do all of these things, but I don't want to do that if there's a preferred way to write mult-site plugins.
Thanks for any advice.
As you know the main deference between developing a single WordPress plugin vs. a multisite plugin is where data is written. According to this article since plugins normally have complete reign over one database, and in some cases the plugins local pages files you must be sure to only call the $wpdb global object instead of a hard-coded reference to a table.
For example, if you need to execute an SQL command on the posts table, then do not use wp_posts as the table name $post_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM wp_posts"));
use this
$post_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts"));
Please see the article for further info.

Dynamically change url or WordPress theme if UserAgent is iPhone

Is there any way to do this?
My website is http://kennethreitz.com. Its driven by some moderate PHP on top Wordpress.
My options are to detect if the user is using an iphone, and if they are either
a) tell wordpress to load a different "theme" that i have written.
b) if this isn't possible, have a different wordpress installation on a subdomain (i.kennethreitz.com) that operates out of the same database, that uses a different theme.
I'd rather be able to do A for SEO reasons.
Any ideas?
http://www.nathanrice.net/blog/serve-ie6-visitors-the-default-wordpress-theme/ demonstrates how to use template filter to dynamically change the WordPress theme (in this case IE6, but it could be for a mobile user agent):
add_filter('template', 'serve_default_to_iesix');
add_filter('option_template', 'serve_default_to_iesix');
add_filter('option_stylesheet', 'serve_default_to_iesix');
function serve_default_to_iesix($theme) {
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false)
$theme = 'default';
return $theme;
}
Have you looked at iWPhone?
It's a Wordpress plugin and theme that automatically takes care of detecting whether the visitor is from an iPhone and formats things appropriately. Pretty easy to substitute your own custom iPhone CSS if you want, although the basic theme is pretty decent.
There's also WPTouch which looks to be similar in functionality but is a bit more recent and has better administration integration.
Here is a Wordpress plugin which serves different themes to different browsers (e.g. iPhone):
http://code.kuederle.com/browserbasedthemes
I don't have experience with WordPress, but the iWPhone plugin + theme looks like it might work
Out of the box you cannot do either A or B.
Loading a different theme is not possible as that option is stored in the database under the wp_Options table. This setting is site wide not per user. Option B is not an option either since you would be sharing the same database you would be selecting the same value for the theme.
If you look in the wp_options table the theme setting will be found in the record that corresponds to "template" and "stylesheet".

Categories