How to store a few values in Wordpress database - wordpress

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 ;
}

Related

Does a multisite wordpress program run plugin instance per site?

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.

how to store custom plugin data to database without using custom table in wordpress

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.

What is best option to to create custom database table while developing WordPress Plugin?

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.

Importing excel files into Wordpress custom post

I'm starting work on a new Jobs website (where client can create new jobs through the WP backend). This is easy enough with setting up with a custom plugin like post-meta
Wordpress - post meta plugin
Which I've setup with custom post meta fields (location, sector, status etc.)
However when I create jobs and fill out the post meta fields and check the DB they all have array encoding:
E.g. a:1:{i:1;a:1:{i:1;a:1:{i:0;s:11:"Sydney City";}}}
How do I remove this so it simply says 'Sydney City' for the meta key value?
But even if I fix that, the client needs to send me a spreadsheet with all the jobs, how do I import something like that into the DB? For example how can a spreadsheet like this:
Job Name,Advertiser,Status,Start,Finish,Category,Suburb,State
Value 1,Hays Constr,Tender,12/3/13,12/4/14,....
Be imported into the meta_key, meta_value architecture? Is it even possible?
What do you think would be the way to proceed? I do want the client to be able to edit Jobs in the WP Admin... but I also need to import a ton of jobs and surely there's a quicker way to do this

How can wordpress themes save and retrieve settings?

I'm programming a wordpress theme and need to make it save data, how should I have it do this? Is there a wordpress function or would I have to connect to the database on my own?
Are you just trying to store simple name -> value pairs?
You could check for $_POST data in your theme then use update_option($name,$value) to save the data.
update_option will create the row in the DB if it doesn't exist. And get_option($name) will retrieve it.
Or are you trying to store something more complex?
It depends on what kind of data you're trying to save, how you want it saved, and what you want to do with it later. Can you be more specific?

Resources