Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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) {       
        if(isset($name)):
            $options = get_option(THEME_SPACE . '_options');
            $option = $options[$name];
            echo $option;
        endif;
    }

}

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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.