Class for adding options to the site

Preface to the lesson

Working with CMS WordPress, I really liked the idea of ​​options in plugins., which can be used as in the admin panel, and on the user side. These options can be placed as technical information during development (database connection data, store a list of custom fields, store options that apply to particular data types), and some static data that is displayed on all pages (address, phone, Name of the site, slogan). These options are very useful for displaying contact information., that don't change often, but they are often located in different parts of the code and it is very inconvenient to search for them.

From words to deeds

I think we have finished with the introduction and the first thing I will give you is the code of the class itself.

This is such a difficult class., let's look at all its methods in order.

Class constructor - checks, does the database have an options table, if she is not, then independently creates this table. The constructor accepts a database connection variable $db and a table prefix variable $prefix. If there are no $db or $prefix variables, then an error message is written to the $error property and the program stops.

Method get_option() needed to display one option that is already in the database. Here is the usual selection, think, if you work with OOP, then you can no longer talk about it.

Method add_option($option, $value) needed to add new options to the database, if an option with the specified name already exists, then to the property $this->error a message is added, which the developer can display for himself or for site visitors using the method log().

Method is_name_options() helps to check, is there an option with the suggested name in the options table.

Method update_option() updates data in already existing options. Accepts 2 required parameters $option and $value (option name and value).

class method delete_option() needed to remove unnecessary options, if it exists in the database. The option is checked and if found, then removed from the database by name.

Method load_alloptions() designed to display all options on the screen, this function is needed solely for viewing options by the developer.

About method log() we already said, this method is for outputting all messages, that got into the property array $this->error.

Working with the Options Class

Now, when we have considered all the methods, we can smoothly move on to working with our class.

Since the class works for us through the database, then first we need to create it and connect, and the table will be created by our class.

Next, we connect our class and try to use it:

Play around with the class methods and try adding new options using the methods, display them on the screen, update data in options, remove options. Be sure to try displaying messages using the method log().

That's the whole general overview of the class, I think you'll figure it out. To work with the example, you must enter new data to connect to the database. I put the class itself in a file, so that you don't get errors while copying, but be sure to understand its operation before using.

Скачать архив с примером

If you find inaccuracies, write us in the comments and we will try to fix them.


Buy WordPress hosting
/* 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...

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