0

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?

2 Answers 2

1

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.

1
  • Thats great. I never knew they were stored in the cache. Thanks for the help toscho & Ludovic. Aug 24, 2012 at 21:37
0

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

1
  • I have edited my code above using the Options API. But I think it will be accessing the database unnecessarily too many times. Aug 24, 2012 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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