How can wordpress themes save and retrieve settings? - wordpress

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?

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.

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

Drupal 7 - do something in a taxonomy hook after the database has been updated

I would like to do something after a taxonomy item has been inserted or updated. I tried using hook_taxonomy_term_update and hook_taxonomy_term_insert. However, anything you put here will execute before the database is updated. How can I do something after the database has been updated?
Specifically, what I'm doing is updating a text file that lists all the taxonomy terms. I pull this information from the database, so the db needs to be up to date before I write to the text file.
what you could use is
drupal_register_shutdown_function()
another option is to use module hook_post_action

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.

Wordpress wp_options option question?

Hello
Can anyone explain me what this mean :
a:2:{s:8:"scSlider";s:8:"featured";s:8:"npSlider";s:1:"4";}
How should I read these values?Tnx
Those are serialized options. When you use add_option() or update_option() with an array or object instead of a scalar value, WordPress serializes the data with serialize() before it puts it in the database. It's incredibly handy when you don't want to have to create a lot of different options in the database to save some values. This way you just put them all into an array and just save the array.
When you retrieve the data with get_option() WordPress will automatically unserialize the data into an array or object as well.
That looks familiar: it looks like a plugin option. As Pekka suggests, they use serialised arrays to pack multiple variables into a single database entry. (Use unserialize.)
I'm guessing you found it whilst doing something like:
SELECT *
FROM `wp_options`
WHERE 1
LIMIT 0 , 30
Unfortunately, without some idea of which plugin you have installed (I'm guessing it's a slider widget of some sort) it's unlikely that anyone will be able to tell you what the values mean.
Could I also suggest making use of the excellent Wordpress Stackexchange site?

Resources