-1

I have the following configuration:

$GLOBALS['x'] = 1;

add_filter( 'filter_hook', 'my_function', 10, 2 );
function my_function() {
    $GLOBALS['x'] = 2;
}

add_action( 'action_hook', 'my_function2', 10, 2 );
function my_function2() {
    echo $GLOBALS['x'];
}

OUTPUT:

1

I know, that the action hook is executed always after the filter hook.

I except 2 as the result.

EDITED: I have to use these two hooks, as these have several other tasks to do. So the above code is just a sceleton. My problem is that I need to forward data to the action hook.

My question: Is there any scenario, which can cause this result?

Any help would be highly appriciated.

closed as off-topic by Mark Kaplun, Jacob Peattie, Christine Cooper, EAMann Mar 13 '18 at 12:54

This question appears to be off-topic. The users who voted to close gave these specific reasons:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • What are the actual hooks you’re using? – Jacob Peattie Mar 10 '18 at 2:45
  • 2
    your problem start with using globals in the first place, but regardless, if you are going to ask questions about you code you need to show it, and not only 50% of it and let people guess the other part – Mark Kaplun Mar 10 '18 at 4:29
  • @MarkKaplun You are right from one aspect, but there is other too. If somebody read the question and understood it as well, he/she didn't give a thumb down. The question was the bold typed phrase (I've just edited). To answer the question the answerer doesn't need more, than the basic info, I've provided. – Geeocode Mar 10 '18 at 22:46
  • @JacobPeattie These are Gravity Form hooks: add_filter( 'gform_pre_render',) and add_action( 'gform_pre_submission',). – Geeocode Mar 10 '18 at 22:50
  • 2
    I would guess by the hook names that one is triggered when the form is rendered, the other when it's submitted. Sounds like you have two separate http requests. – Milo Mar 10 '18 at 23:07
1

In my opinion you shouldn't use $GLOBALS at all. add_filter shouldn't be used the way you are using it.

So there is apply_filter in wordpress that returns a filtered value such that

$my_variable = apply_filter('my_filter_tag', 'default_value', $arg1);
  • In the above code 'default_value' can be anything.
  • $my_variable will hold the value 'default_value' if no one called add_filter('my_filter_tag', 'my_function_name', 10, 2)

When someone calls add_filter('my_filter_tag', 'my_function_name', 10, 2) what this means is that

$my_variable = my_function_name('default_value', $arg1); Here the value of $my_variable will be equal to the result of the my_function_name because we registered my_function_name to be called every time the value of $my_variable is being set.

To achieve the desired effect described in the post you can do the following:

# This will set the value of $GLOBALS['x'] = 1 by default if there are no hooks called.
# $arg1 is optional but since you specified the value 2 when registering the filter I've added it to the example to explain it
$GLOBALS['x'] = apply_filter('filter_hook', 1, $arg1); 

function my_function($x, $y) {
    # Here this function required two parameters
    # This function is being called by the filter_hook
    # So the value of $x will be equal to 1 ( Which is the default value)
    # $y will be $arg1
    return 2; # Notice that we're returning the value rather than setting it.
}

# Here the value 10 means priority and the value 2 means that the function 
#`my_function` will be given two arguments when it is called, the first argument
# for the called function will always be the 'default_value'
add_filter( 'filter_hook', 'my_function', 10, 2 );

add_action( 'action_hook', 'my_function2', 10, 2 );
function my_function2() {
    echo $GLOBALS['x']; 
}
  • Thank you for your detailed answer! My problem is that I have to use these hooks, as I have a lot to do with them. But I have to exchange data between them. That is what i try to achive with $GLOBALS. By the way the methode you proposed above is intresting, but it seems me a kind of global data forwarding regardless we do it with a $GLOBALS[] or with an apply_filter's return value. – Geeocode Mar 10 '18 at 23:41
  • To be clear: My problem above is that $GLOBALS has lost the value I assigned to it, so I except that the result of your solution would be the same in this case. But thank you again for your answer. – Geeocode Mar 11 '18 at 0:22
  • @Geeocode Globals or any variable will loose it's value that you expect it to be if something else is assigning a different/default value just before you check it's value – devkabiir Mar 13 '18 at 2:59
  • To be clear, when you're checking the value of your global variable it's possible that something else is overwriting it just before you check it or access it. – devkabiir Mar 13 '18 at 3:00
  • This is exactly the reason I said that you should't use globals at all. Since you don't know what else is using that same global variable. In this sense Global variables are GLOBAL you can't predict and control their behavior or values – devkabiir Mar 13 '18 at 3:02

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