Get custom tables from database in wp-config.php - php

I want to use another Wordpress installation that gets it's tables from my original Wordpress prefix. I installed both WP installations in the same database with another prefix. I use WP Event Manager and the original prefix is like this:
wp_em_events
I want the same plugin at my new installation to automatically get it's tables from the orginal one, like the following code already does for the users at my wp-config.php:
define('CUSTOM_USER_TABLE', 'wp_users');
Is there a rule I can use at my wp-config.php file like the one that gets the users? or is there another way to get the data from the orginal table to the new one?

Eventually it worked using the following code in wp-config.php:
define('EM_EVENTS_TABLE', 'wp_em_events');
It also works with other (Wordpress) tables:
define('POSTS_TABLE', 'wp_posts');
Pages are also seen as posts. Be sure to get al tables for a plugin, pages, users etc. . In some cases you also have to get the table with the meta information.

Related

Joomla plugin to override table prefix

Overview:
I have a single Joomla website that have various copies of the database tables all in the same database, but with different table prefixes.
Depending on which user is accessing the site, i need to alter which joomla table prefix is used in order to display the relevant tables.
This has to be site wide.
Problem:
with a core code hack i can alter the prefix as needed, but would prefer this as a plugin.
With the plugin i can get the prefix, using $db->getPrefix(), and i can set an immediate instance of the connection using $db = JDatabaseDriver::getInstance( $option ). But this is not site wide and is overwritten by the standard $db = JFactory::getDbo().
So i need a system plugin that will repalce every instance of a table prefix as it is run.
Any ideas?
Sorry but if you install a new component or module you have many problem to synchronize this changes on all DB. I think the best for you is to redirect each user to his real site (many joomla file with many db).
Otherwise you can create a personal component and read table prefix of that user and display only date that you want, in this way just duplicate the tables you need and not all the joomla structure.

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!

Two wordpress sites using diiferent themes on the same database

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.

website links with string auto attached

I made a new website for someone but when i tried to launch it live i had problems with links. The old version of the site had language extension.
Now every link with the domain:
sites.com get extension ( ex: www.site.com/**es**/; wwww.site.com/**en**/,www.site.com/**es**/contact)
Even thow i made dynamic links i have problems accessing the wordpressadmin and also i have problems with referencing the homepage from other pages (only way i was able to to refence the homepage was by making something like site.com#).
After every change in wordpress admin the links are adjusted with language extension/string.
To remove all language extensions you could run a query in the form:
UPDATE wp_posts
SET post_content = REPLACE(post_content, 'site.com/es/', 'site.com/')
WHERE post_content LIKE '%site.com/es/%'
On your database for each language extension. This will remove all references in post content. You'll also want to check your wp_options table, replacing finds in the option_value field.
Finally run a check on the wp_postmeta table - many items will be serialised values and so can't be changed with a simple find/replace like above. Instead you should run a find, then figure out how to change any found values via the Wordpress admin panel.
As always back up your database before playing around.

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.

Categories