WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make a php class that gets an option from the wp database (which is an array of options) and be able to call this class with the name of the option and the class to return the options value.

Does anyone know how would I go about doing this?

EDIT: I have tried the following code

class N_Options {

    function __construct($name) {       
            $options = get_option(THEME_SPACE . '_options');
            $option = $options[$name];
            echo $option;


And I have tried to call the option using

new N_Options('option');

This retrieves the option from the database just fine now. However, I think this may be accessing the database too many times as I will be accessing this class over and over for all my options.

Is there a way to store these options inside the class as an array variable and keep calling the class to look inside the variable without calling a new class for each option?

share|improve this question
up vote 1 down vote accepted

If you have registered your option without setting the fourth parameter $autoload to no your calls to get_option will not trigger any extra database call because all aotoload options are stored in the cache when the site is loaded.

To test it add …

define( 'WP_DEBUG',         TRUE );
define( 'SAVEQUERIES',      TRUE );

… to your wp-config.php, install the plugin Debug Queries and look at the real queries.

share|improve this answer
Thats great. I never knew they were stored in the cache. Thanks for the help toscho & Ludovic. – Carl Thomas Aug 24 '12 at 21:37

look at Options API in wordpress codex, there is all you need i think...

share|improve this answer
I have edited my code above using the Options API. But I think it will be accessing the database unnecessarily too many times. – Carl Thomas Aug 24 '12 at 21:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.