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

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.

Related

Serialized data in Wordpress form is "serialized" again when saved in mysql

I have a challenge with the way data is stored from Wordpress into mysql.
I'm new to Wordpress - but I'm trying to combine different plugins to achieve a specific functionality on a website.
Plugin #1 uses a specific type of post and some metadata - the plugin works great.
Plugin #2 can create posts of custom types and with custom metadata from the frontend. A flexible and great plugin.
My intention is to use plugin #2 to create posts from the frontend for plugin #1, which requires that some specific metadata contains specific values (otherwise the data are interpretted wrong). These values are saved with hidden inputfields in a form.
My challenge is that a value (which is serialized) is kind of serialized again. The value is a:1:{i:0;s:3:"198";}
Plugin #2 handles it well (as the data is shown correct in front- and backend of the plugin) - BUT in myPhpAdmin I can see that the data is saved differently s:20:"a:1:{i:0;s:3:"198";}"; - and hence that value cannot be understood by plugin #1.
I'm thinking of two options:
change something so s:xx: isn't added to the value(-s)
make the frontend form/plugin save the data in a serialized way so the value gets the correct format.
Any recommendations? - can I in anyway achieve my functionality?
Looks like
s:20:"a:1:{i:0;s:3:"198";}";
value is being serialized twice. You can check the plugin source code to see why the plugin serializing same value twice and if can prevent it than both plugin can share same data.

WordPress inquiry form without saving into DB

I need WordPress plugin for inquiry form, which send data to mail only. Don't want to save into DB because large database needed to store into database
You can use the https://wordpress.org/plugins/contact-form-7/ and with help of this plugin, you can create custom fields as per you need an inquiry form.
In future, if you need to store data in DB then there is an additional plugin
https://wordpress.org/plugins/cf7-database/
https://wordpress.org/plugins/save-contact-form-7/
https://wordpress.org/plugins/contact-form-submissions/

How to store a few values in Wordpress database

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

Importing data from spreadsheet into wordpress DB, along with custom taxonomies and their terms

I am importing a large DB from an Excel spreadsheet into a wordpress DB. I was going to do it manually by creating a new table but decided to import it into the existing wordpress framework as it will allow me to change information manually in the wordpress back end.
I was importing all 2000 rows into the wp_posts table when I realised this table doesnt have a term column. The terms are stored in other tables.
My question is... how can I import the spreadsheet into the database and keep all of the important data, especially the terms, as my site queries all information by the "term" and uses jQuery tabs to display the different terms as sub-categories.
My information looks like this:
post-title(title)---category(term)---description(content)---imageUrl---price
Note: I will create a seperate column in the wp_posts table to handle the price, or any other information I need bringing in, Im just unsure about how I can get the term information into the corresponding tables...
Thanks :)
Don't insert the data directly into the table. Call the wp_insert_post() function and pass in the correct values. It will take care of all the db dependencies.
To insert the meta data, use the add_postmeta() or update_postmeta() functions.

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