Home » WordPress » WordPress get_option Example

About Era Balliu

Era Balliu
Era is a Telecommunications Engineering student, with a great passion for new technologies. Up until now she has been coding with HTML/CSS, Bootstrap and other front-end coding languages and frameworks, and her recent love is Angular JS.

WordPress get_option Example

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 get_option.

How can you do this exactly? Look below!

 
 
 

Syntax

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 $option and $default, though only the first one is obligatory.

The $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.

The $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 admin_email, blogname, blogdescription and start_of_week to date_format, home, siteurl and users_can_register, all of which are located in the General Options.

The are other options such as blog_charset and 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.

Usage

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:

get_bloginfo('wpurl');

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: get_site_options().

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.

Syntax

The 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.

Usage

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 siteurl option.

$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 really?.

Download the source code

This was an example of get_option in WordPress.

Download the source code for this tutorial:

Download
You can download the full source code of this example here : GetOption
(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Wasim Recent comment authors

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

  Subscribe  
newest oldest most voted
Notify of
Wasim
Guest

Hi,

So if I want to retrieve options from the single WordPress website then I will need to use get_option() function, and when I need to retrieve options from multisite then I will need to use get_site_option() function, right?