• Resolved danifola

    (@danifola)


    I have defined a global variable on a code snippet. I defined it at the start of the code and inside the function there defined.
    I then coded a second snippet that has a different hook and action. Even though I define the custom global variable it does not have the value I put on the first piece of code.
    So basically code snippet 1 is assigning a value to a global variable and code snippet 2 is reading the value. But when it tries to do so, the value si empty.
    Any ideas as to what I am doing wrong or where I need to do something else for this to work?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Are you able to provide an example of how you are doing this in the two snippets?

    Thread Starter danifola

    (@danifola)

    Snippet1:
    global $test;
    test = “DONE”;
    Snippet2:
    global $test;
    echo “TEST “.$test;

    To test I defined the snippets as execute once. I executed first Snippet1 and then Snippet2 and I am seeing just TEST.

    What am I missing here?

    Maybe priority problem?
    The parent snippet must have lower priority as the child.
    I mean as parent the code, which some other code might use.

    This is not a serious case, but because of missing priority check I crashed my site.

    Plugin Author Shea Bunge

    (@bungeshea)

    @danifola

    Execute once only applies to the next page load. PHP variables don’t stay defined across page refreshes.

    To test this sort of thing, you would need to store the data persistently, like in an option:

    Snippet 1:

    add_option( 'my_opion', 'test' );

    Snippet 2:

    $test = get_option( 'my_option' );
    echo 'TEST: ' . $test;
    Thread Starter danifola

    (@danifola)

    That’s what I was looking for. Thanks Shea

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom global variable not usable on second snippet’ is closed to new replies.