WordPress has simplified a lot the way we approach functionality, as it has defined many PHP functions, some of them (known as Template Tags) intended especially for WordPress Themes.
Among many others which you can find here, are the Options functions. In case you want to get values for a specific option of your site from the database, you can use
How can you do this exactly? Look below!
The function gets the desired option’s value from the options database table and returns it, or in case the value is not found it returns
FALSE. The function is called through PHP, similarly to the code snippet below:
<?php echo get_option( $option, $default ); ?>
You can easily see that it takes two parameters, namely
$default, though only the first one is obligatory.
$default parameter is what you want the function to return in case the value you are searching for cannot be retrieved from the database. It is optional, and in case that you do not specify it, it will be
FALSE by default.
$option argument is obligatory, and with it you specify the option for which you want the database data. It can be a whatever, starting from
users_can_register, all of which are located in the General Options.
The are other options such as
posts_per_page, set in Reading Options, and also
template, set in Presentation, which shows the current theme’s name. Of course, there are many more that the argument
$options can be, both built-in and depending on the plugins you use.
You can use the
get_option() function as a first step for whatever you need to do with the value of the option after retrieving it, be it just that or even attaching it to a variable and putting it out for your users to read.
You can display data about your blog using the code snippet below:
<h1>Some option values of <?php echo get_option( 'blogname' ); ?></h1> <p>Character set: <?php echo get_option( 'blog_charset' ); ?> </p> <p>Blog Description: <?php echo get_option( 'blogdescription' ); ?> </p> <p>Theme Name: <?php echo get_option( 'template' ); ?> </p> <p>Site URL: <?php echo get_option( 'siteurl' ); ?> </p> <p>Default Upload Location: <?php echo get_option( 'uploadpath' ); ?> </p> <p>Date Format: <?php echo get_option( 'date_format' ); ?> </p>
The function set in the h1 heading will put out the blog’s name. In the next part we have displayed other information about the blog, each characteristic put into a separate paragraph. Everyone of them is pretty self-descriptive. However you must note that the
'siteurl' will not return te homepage URL, but instead it will give you the same result as:
This is how you use the
get_option() function in WordPress.
Get Site Options?
It happens very often that the
get_options() function gets confused with another very similar one:
These functions are almost the same in that they both return the options value, only
get_site_options() will return the value of the network-wide option, when there is a multi-site installment. In non multi-site installments, they’re practically the same.
get_site_options() function is used like below:
<?php get_site_option( $option, $default , $use_cache ) ?>
You will notice that the first two arguments are the same as in
get_option(). The only catch is that
$option is not expected to be retrieved from the SQL. The only “extra” argument is
$use_cache which takes a boolean value for whether to include the cache or not, and is by default set to
TRUE. You might already guess that it has no effect on non multi-site installments.
Let’s see some very basic examples of this function:
echo get_site_option( 'siteurl' );
This line of code would give us the network-wide value of the
$value = get_site_option( 'this_is_not_an_option' ); $value = get_site_option( 'this_is_not_an_option', 'really?' );
In the first line, since
this_is_not_an_option is not a valid option, we would get the default return value of
FALSE. However, in the second line, this default value is replaced with
really?. Which means that since the first argument is not valid, we will get a return value of
Download the source code
This was an example of get_option in WordPress.
Download the source code for this tutorial:
You can download the full source code of this example here : GetOption