4

get_option() provides couple of filters 'pre_option_'.$option and 'option_'.$option.

However most times I tried to make use of these it usually explodes and not worth the trouble - either I need to check another option inside my filter, which triggers my filter, which triggers my filter...

Another common case is that I need to get current option that I am filtering and I cannot do that because I am filtering it.

Just curious - is there some practical logic to follow here? I know I could juggle my filter, but that is overhead that I don't like and filter removal is considered not too reliable by some. ;)

For recent practical example - I want to filter posts_per_rss to my option, but provide WordPress value if my option is not set (for the record I know that recommended way to mess with it is via post_limits).

8
  • You could use a static variable as a flag to control access to your filter function - or are you looking for a way to not trigger the filter at all?
    – anu
    Commented May 7, 2011 at 15:54
  • @anu simply put I am looking for a way to circumvent recursion here without losing simplicity. I like to tinker robust and reusable extensions to native stuff for specific purposes.
    – Rarst
    Commented May 7, 2011 at 16:02
  • 1
    @rarst Setting a flag could work then - don't think that adds too much complexity, although you would need to add a test at the start of your filter function, and set and unset the flag.
    – anu
    Commented May 7, 2011 at 16:06
  • Aren't some options autoloaded? For the other cases i understand your problem and even after thinking about for an quarter hour, i have no idea how it's meant to be done in case you need another option. The @anu flag surely is an option, but i guess the pre_option_.$option filter is meant to completely override the db-query, so maybe the wp dev thought you will then just add multiple queries instead of the default one. Btw: I hope you don't mind - I slightly modified the Q. :)
    – kaiser
    Commented May 7, 2011 at 18:42
  • 1
    @kaiser I don't think autoload affects retrieval process and filters, it merely pre-caches option. Option will still pass through filters, the only difference is that it will come from cache rather than database first time.
    – Rarst
    Commented May 7, 2011 at 19:36

1 Answer 1

10

Usually I remove the filter, then add it back on afterwards;

function _my_custom_option( $option )
{
    remove_filter( 'pre_option_name', '_my_custom_option' );

    // do what you like with $option

    add_filter( 'pre_option_name', '_my_custom_option' );
    return $option;
}
add_filter( 'pre_option_name', '_my_custom_option' );
1
  • if i would've seen something like that outside the context of this discussion i wouldn't know what to think about it ... and likely have missunderstood it. At least add_filter just adds an array to an array, so i can be done and will work... +1 for beeing extremly tricky.
    – kaiser
    Commented May 7, 2011 at 19:10

Your Answer

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

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