What is WordPress get_option function?

1055 0

WordPress has defined an overwhelming number of functions for website developers and even for the developers of themes and plugins. get_option belongs to those basic functions which a website designer probably use but a plugin or theme developer must use this to make their life easy and save their a lot of time. In this article, we will first discuss what are WordPress functions than What is WordPress function get_option and how to use it?

WordPress Functions

Many of the core WordPress functions are useful to Plugin and Theme developers. The files of WordPress define many useful PHP functions. Some of the functions are designed specially for WordPress Themes, some for WordPress Plugins and rest are used to create core WordPress functionalities.There are other functions related get_option like add_option(), update_option(), delete_option() etc that you might be looking for. If that is the case, have a look at the list of WordPress functions to find out more functions. However, we further discuss the implementation and call of get_option.

Function get_option()

I strongly recommend you to go through the official documentation on get_option on.

The actual implementation of get_option looks like:


get_option( string $option, mixed $default = false )

Where:

$option is the option whose value we want to extract; and

the second argument forces the default value to be false.

These parameters are explained in detail below.

Parameters

  • $option

The first argurment passed to get_option is $option which takes the desired option you want. There are a lot of valid arguments that can be passed including:

  • 'admin_email' – E-mail address of blog administrator.
  • 'blogname' – Weblog title; set in General Options.
  • 'blogdescription' – Tagline for your blog; set in General Options.
  • 'blog_charset' – Character encoding for your blog; set in Reading Options.
  • 'date_format' – Default date format; set in General Options.
  • 'default_category' – Default post category; set in Writing Options.
  • 'home' – The blog’s home web address; set in General Options.
  • 'siteurl' – WordPress web address; set in General Options.
  • etc.
There are many more options available, a lot of which depend on what plugins you have installed.

  • $default

If the value of option is not found than the default value false will be returned.

Return Value

If the option does not exist or does not have a value, then the return value will be false. This is useful to check whether you need to install an option and is commonly used during installation of plugin options and to test whether upgrading is required.

If the option was serialized then it will be unserialized when it is returned.

Any scalar values will be returned as strings. You may coerce the return type of a given option by registering an ‘option_$option’ filter callback.

Source Code

The complete source code of get_option, its supporting functions and actual working algorirthms running in get_option can be found here.

Conclusion

In this article, we have discussed what does get_option() do, how to use it and where can you use it. get_option is one of the mostly used functions of WordPress. You may find other useful WordPress functions here. Hope you liked the article. Keep visiting artzstudio.

In this article

Join the Conversation