WordPress Options

Everything up until this point applies to PHP coding in general and is not specific to WordPress. With that said, some of the functions that I use from this point on will be specific to WordPress, so these examples will only work correctly inside of a WordPress site!

By itself, PHP is not able to permanently save data that is stored in variables in a script. To accomplish this, you must use a database.

Database usage with PHP could be a completely separate training course all by itself, so my intentions here are to simply show you how you can use databases within WordPress with much less work than it would taking otherwise.

One big part of the ease when using WordPress is the fact that it has built-in security features that will help to protect your website from malicious input. Whenever you accept raw input from a website visitor and try to save that information to a database unaltered, you put your entire site at risk to being hacked. However, if you follow the recommendations of this guide, you won't have to worry about that because it uses WordPress functions that are protected from this type of website attack.

If you remember in the very first training session on cPanel, I showed you how to create a database table. That table could be used to store information using a PHP script. However, before I get into showing you how that works, I want to show you an even easier method to save data with WordPress.

This easy method is using WordPress Options. This is actually a pre-built database table in all WordPress sites that can be easily used with pre-built WordPress PHP functions.

To start, I just want to tell you the 4 basic option functions that are available and what they do:

add_option - Create a new option and value in the database.
get_option - Get the value of an option from the database.
update_option - Update the value of an option in the database.
delete_option - Permanently delete an option and value from the database.

add_option

First, let me show you how to create a new option. This is done using the add_option function.

This function requires two arguments: a name and a value.

The option name needs to be a unique name (use the prefix naming procedure here too, like I recommend doing for function names). This name is used to refer to the option - when you create it, get the value, update the value, or completely delete it.

The value for the option is whatever information you want to save.

With that said, here is an example of this function:

<?php

//Declare text (a value) for the variable $my_value $my_value = 'This is the information I want to save.';

//Create a new WordPress option by the name of my_custom_option_name //The value is set to the value of the variable $my_value add_option('my_custom_option_name', $my_value);

?>

With just two lines of code, I have created information and permanently stored it in a WordPress database.

You should already be familiar with my first line of code - I am declaring a text value for a variable that I have named $my_value.

The second line of code uses the WordPress function, add_option. Note the opening and closing parenthesis following the function name, which is then followed by a semicolon.

Between the parenthesis for the add_option function are the arguments.

The first argument is the custom name of the option. This is the name that I will use in the future to retrieve the value that I have saved. For this tutorial, I will use the custom option name of my_custom_option_name.

The second argument is the value that I want to save for this option name.

Note that I have enclosed the option name in quotes for the first argument and used a variable for the second. Either is a correct way to pass information for an argument - it just depends on where the information is coming from. Typically, you will know the option name but you may not know the value that you are saving because it has come from your website visitor, so this is the way that I have shown it in this example. We will get to a complete working example by this end of this training session.

get_option

Next, I want to show you how to retrieve the saved information for an option you've created. This is done using the get_option WordPress function.

To use get_option, all you need is the option name that you used when you created the option.

Here is an example of this function:

<?php

//Retrieve the value of the custom WordPress option, my_custom_option_name //Store the value in the variable $my_value $my_value = get_option('my_custom_option_name');

//Output the variable value to the browser echo $my_value;

?>

In this example, I start by retrieving the value of the option I created before and save it to a variable.

The get_option function only has one argument - the custom option name that was used to create the option I am retrieving.

I have put my_custom_option_name in quotes (the custom option name I used in the previous example), inside of parenthesis for the get_option function (and closed it with a semicolon).

Then, I simply output the value of the $my_value variable to the browser using the echo command. This step isn't necessary but just something I have done to show an example of what you can do with the information and/or how you can view it.

update_option

If you ever want to change the saved value of a saved option, just use the update_option WordPress function.

This function works identical to the add_option function (except the function name is different obviously - the arguments used are identical).

Here is an example of the update_option function:

<?php

//Declare text (a value) for the variable $my_value $my_value = 'This is the new information I want to save.';

//Update our WordPress option by the name of my_custom_option_name //The value is set to the value of the variable $my_value update_option('my_custom_option_name', $my_value);

?>

As you can see, the code above is nearly identical to the previous example for add_option, except the function name is different here. I still use the option name as my first argument and the option value as the second argument.

delete_option

If you want to permanently delete an option that you created, use the delete_option WordPress function.

This function uses the same argument as the get_option function - just the option name.

This function is NOT to be used to simply clear the value of an option you have saved - this completely removes the option name and value from the WordPress system! If you decide later to use the same option name again, you must create the option again.

The example below shows the delete_option function in use:

<?php

//Permanently delete the custom WordPress option, my_custom_option_name delete_option('my_custom_option_name');

?>

With this function, simply provide the option name that you want to delete and it is gone forever! 


© PHP Training Guide

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (24.08.2014)
Views: 392 | Rating: 0.0/0
Total comments: 0
avatar