get_option() filtering and getting out of recursion

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).

1 Answer
1

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' );

Leave a Comment