Storing data in wordpress

The task of saving data is worth it every time you need to program something., and when developing plugins, this is one of the first tasks after creating the interface part. But data in WordPress can be stored in different ways. I offer two options.

  1. Saving data with the add_option function for options.
  2. Adding data to a new database table to store large amounts of data and those that can change all the time.

Consider how to properly store options.

Place the code in the plugins folder in a new file named add-my-options.php.

After placing the code, activate the plugin in the admin panel and if everything is done correctly, you will see the item Add data in the admin menu on the left.

Consider the code of the written plugin in order

Function wp_nonce_field(‘update-options’) adds two hidden fields that allow you to automatically check the user's rights to change settings and redirect him back.

Function get_option() displays the saved data in the input field. Another piece of code located at the end of the form is responsible for writing options from the input fields.

«page_options» contains a comma-separated list of all settings, to be recorded when saving.

On the official site WordPress found another example of data storage. It seems to me, it will be more correct as there is an explicit separation of form template and code. Code looks much cleaner. I won't describe everything, everything is clear from the code, if you don't understand write in the comments, I will try to explain.

I did not have time to check the last example, but the first one works stably. I will try to describe working with databases in the next issues..

Good luck with plugin development!


Купить хостинг WordPress
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Data storage in WordPress

The task of saving data is worth it every time, when you need to program something, and when developing plugins, this is one of the first tasks after creating the interface part. But data in WordPress can be stored in different ways. I offer two options.

  1. Saving data using the add_option function for options.
  2. Adding data to a new database table to store large amounts of data and those, which can constantly change.

Let's consider how to properly store options.

Let's place the code in the folder with plugins in a new file with the name add-my-options.php.

After placing the code, activate the plugin in the admin panel and if everything is done correctly, you will see an item Add data in the admin menu on the left.

Let's look at the code of the written plugin in order

Function wp_nonce_field(‘update-options’) adds two hidden fields, which allow you to automatically check the user's rights to change settings and redirect him back.

Function get_option() displays the saved data in the input field. Another piece of code, which is at the end of the form, is responsible for recording options from input fields.

«page_options» contains a comma-separated list of all settings, which must be recorded when saving.

On the official website WordPress discovered another example of data storage. In my opinion, it will be more correct as there is a clear separation of form template and code. The code looks much cleaner. Describe not as if everything is clear from the code, if it is not clear, write in the comments, I will try to explain.

I did not have time to check the last example, but the first works stably. I will try to describe working with databases in the next issues.

Good luck with your plugin development!


Купить хостинг WordPress
WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

1 Response

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal