First, I would like to apologize if my question isn't clear enough - I'm new to wordpress so I might have some issues with the nouns.
Second, here's a description of my problem:
I have a plugin which creates tables in the DB.
What I want to accomplish is to create a multisite solution in which the same plugin is installed on all sites but each site see a different instance of DB.
This will allow me to have a some sort of multi tenant solution without re-writing the plugin.
Is this possible?
I'll appreciate your help.
Nadav
Edit:
That's the code I have in the Base Entity of the plugin:
public static function getTableName()
{
global $wpdb;
return $wpdb->base_prefix . static::$table;
}
Should I change it to the following in order to support table per site?
public static function getTableName()
{
global $wpdb;
return $wpdb->prefix . static::$table;
}
If you are using $wpdb->prefix when creating your tables, then you will already have separate tables for each site.
global $wpdb;
$my_table = $wpdb->prefix . 'my_table';
$query = "CREATE TABLE $my_table...";
The above code would result in a separate table for each site, with names like wp_2_my_table, wp_3_my_table, etc (assuming you are using the default base prefix wp_).
The majority of plugins do not have to be updated to support multi-sites explicitly, so long as they adhere to best practices.
Conversely if you wanted to create a single table for all of the sites in the network, you would use $wpdb->base_prefix.
Just make sure you consistently use the correct prefix when creating or accessing tables.
https://codex.wordpress.org/Class_Reference/wpdb#Class_Variables
Edit:
Should I change it to the following in order to support table per site?
If you want separate tables for each site, then yes you will need to change it to use $wpdb->prefix rather than $wpdb->base_prefix. Note that if you're creating the tables in your activation hook, you'll have to re-install the plugin in order to create the new tables.
Related
My Wordpress website needs to retrieve a handful of strings and integers from another website and use the retrieved values when constructing every page.
I am planning to use WP Cron facility to run a PHP function to retrieve the values. I'd like to store them in the Wordpress database. Is there a standard way of doing this? Should I create my own table for this? Use an existing table? What Wordpress APIs should I use to read and write the data?
Thank you for your help!
I think you should create your own table , and to connect to word press database , you should listen to action hooks then in function.php file you will find $wpdb; global object you can connect to database using this object
Example in function.php file
function doSomeThing(){
global $wpdb;
// write your logic here ;
}
I noticed that all of the tables in PHPMyAdmin start with "wp_". I want to make a new table to populate with user uploaded images tied to their posts, so can I just name it anything or is it necessary to name it with "wp_"?
Could you? Yes. Should you? No! It's a good practice to use the common prefix for any custom tables.
The table prefix (wp_ by default) is set in your wp-config.php file. Users have the ability to change this prefix themselves.
You'll want to create the table using the same prefix and then take advantage of the database class in WordPress. For instance if you name your table wp_user_images you could refer to it with $wpdb->prefix . 'user_images' (making sure you have access to the wpdb global first).
Also keep in mind that if you're writing code which you plan to distribute you'll need to create the tables programmatically. Some users may have multiple installations of WordPress in a single database with different prefixes providing separation. If one of those users ran your code they would have multiple sites sharing a single table.
I am developing a wordpress plugin , where I need to store some custom data to database .
Custom data what I want to store like "class title" , "numeric name of class (it will be an integer value) " . Site admin could add those data every time he wants.
To get those data from site admin I have created a form. But I am having problem while storing those data to database . I dont want to use custom table . So I used options api for store those data . I used this function to store data
add_option('class_title', $class_title);
add_option('class_numeric', $class_numeric);
But using this method , user could add data to database only once . But I need to store data more than one time with same option name (in this case ,'class_title' and 'class_numeric').
How could I able to store those custom data to database with same option name more than one time using options api or any other way without custom table. If there are not any other way , then I will use custom table .
You can add a post type, but you could also serialize your option, ie. storing it as an array of array
myoptions = array (
0 => array ('class_title' =>$class_title, 'class_numeric'=>$class_numeric ) ,
1 => array ('class_title' =>$class_title, 'class_numeric'=>$class_numeric ) );
Depending of what you want to achieve, both solutions have their advantages. The second one would be better from a performance point of view.
I think an easy way would be to add a post type.
WordPress 3.0 gives you the capability to add your own custom post types and to use them in different ways.
I am trying to develop a New WordPress Plugin, I have tried to store the settings values in the WordPress built in DB table Options but there is problem storing and fetching the values so i decided to create new database table to store the values, Is this a good idea to create new db table and what is best way to create, sanitize and retrieve the valued.
Rather than write to the Wordpress tables directly, use the Wordpress APIs that serialize your data and write it correctly. The add_option function and the similar get_option function should be what you are looking for.
I imported all wp_users & wp_usermeta from one wp_db (old site) to another wp_db (new site) successfully with sql query & also mapped posts with their respective post_author but old site installed Co-Authors Plus plugin so I also installed Co-Authors Plus plugin at new site but the posts does not shows correct authors
Old Site
New Site
I know this question is old but to solve this what I did was import all the articles using a single author and then in Excel create a line of php code that would specifically add the co-authors for the article. So:
$post_id = 8077;$authors = array( 'Brock');$coauthors_plus->add_coauthors( 8077, $authors );
Replace all the actual data with cell references for ease. Not the prettiest or most fun, but it works and seems to be the best solution using what I've found online. Hopefully you already found an answer, but others may find this helpful.