Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables between hooks in WordPress

Tags:

php

wordpress

I am writing plugin that uses init() action and the_content() filter.

In init I would like to do some cookie checks and set some variables based on result (lets say $mycookieset = 1). In the_content filter I would like to modify article based on $mycookieset variable.

How to pass $mycookieset variable in safe way between these two hooks? I would prefer not to use sessions. It also should be multiple users safe (hundreds of people browsing the web same time).

Any ideas? Thanks

like image 739
Siegfried Avatar asked Nov 28 '25 03:11

Siegfried


1 Answers

You can add the filter inside a function that is hooked into init, and use the cookie value as a variable in the $function_to_add parameter:

    add_action( 'init', 'my_init_function' );
    function my_init_function(){
        // do the cookie stuff
        add_filter( 'the_content', 'my_variable_cookie_func_' . $mycookieset );
    }

Of course, you should have an appropriate callback function for each possible cookie value.

like image 122
diggy Avatar answered Nov 30 '25 16:11

diggy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!