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 am trying to filter some data based on whether a variable from within the function that calls apply_filters() is equal to specific value. That variable is not passed to the apply_filters() parameters. This might explain what I mean:

// function in wordpress core
function get_var_b() {
    // generating $var_a
    $var_a = "a";
    // some code ...
    $var_b = apply_filters('get_var_b', $var_b);
    return $bar_b;
}

// in functions.php
add_filter('get_var_b', 'filter_var_b');
function filter_var_b($var_b) {
    if ($var_a == "c") {
        $var_b = "d";
    }
    return $var_b;
}

I tried to use global $var_a; inside of my function but with no success. The only way I could access $var_a was by editing the core file and passing $var_a to the apply_filters() function like:

apply_filters('get_var_b', $var_b, $var_a);

Is there a way to access this variable without editing the core file or trying to regenerate $var_a from scratch? Thanks.

EDIT: The filter that I am trying to hook to is dynamic_sidebar_params which is called from dynamic_sidebar() function in wp-includes/widgets.php file. I am trying to change the $params[0]['before_widget'] from <aside> to <nav> IF the $id was equal to categories-2.

share|improve this question
    
It would be much easier if you're telling us the actual core function name. –  kaiser Sep 25 '12 at 15:05
    
I have updated the question and add my actual case. –  Ahmad M Sep 25 '12 at 15:26
add comment

1 Answer

up vote 0 down vote accepted

Short answer: you can't.

You're trying to access a variable outside of its scope. In your situation, $var_a is local to the get_var_b() function, so anything outside of that function cannot see it.

This is equivalent to writing this sequence in PHP:

function get_var_b() {
    // This variable is local to the function and invisible outside of it.
    $var_a = "a";  

    // Call a function that returns some value while passing in some default value.
    $var_b = create_var_b( 'default' ); 

    return $var_b;
}

function create_var_b( $default = null ) {
    // Inside this function, we only have access to $default because that's all that was passed
    // in. You can access any set global variables my making an explicit reference to 
    // "global $var", but only if the variable was globalized in its original scope.

    if ($default != null) {
        $var_b = "d";
    } else {
        $var_b = "c";
    }

    return $var_b;
}
share|improve this answer
    
Thanks for quick answer, I thought that my function filter_var_b() will be called from within the core function get_var_b() and hence will have access to its local variables. –  Ahmad M Sep 25 '12 at 15:04
    
Yes and no. The apply_filters() function uses call_user_func_array() to invoke your registered function, pass in the values specified, and return the filtered value within your function. But just because it's invoked from within your function doesn't mean it will have access to your function's scope. It can only see what's in the global scope or what you've explicitly passed in. –  EAMann Sep 25 '12 at 15:07
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.