PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Sharing a global variable between forms

  1. I want to declare a global - $TournamentID to share between forms on the site. The user can select the TournamentID from a dropdown on Form1 and that is set for the rest of the site until it is changed.
    In the pre_render of each other form I want to use this global to know what data to pull back from the database (MySQL).

    I declare $TournamentID in functions.php and have get and set functions for manipulating the global, to save having to declare the global wherever I need it.
    In Form1, the pre_render can set/get the global value no problem.
    However when I go into the pre_render of Form2, this global is null !

    How/where do I declare this global ?

    I know I can pass parameters in the request and that works fine, but a global would save lots of code and make life so much easier.

    I must be doing something fundamentally wrong because I cannot find anything closely related in here or on the web.

    I'm using PHP on WordPress, both up-to-date. The platform is Windows Server 2003 with IIS.

    Any ideas ?


    Posted 9 years ago on Tuesday November 8, 2011 | Permalink
  2. Anybody there ?

    Posted 9 years ago on Saturday November 12, 2011 | Permalink
  3. Can you post the code you are using to declare the value, then how you are accessing it in each form? It has to be something with the implementation.

    Posted 9 years ago on Sunday November 13, 2011 | Permalink
  4. Thanks for getting back Chris.

    I too suspect that there's something fundamentally wrong with what I'm doing, but I just can't see it. I've reduced the code to simplify it. Here's 3 snippets.

    #1 - declaration in functions.php, plus get/set routines.

    #2 - In form #4, after_submission hook (I know this fires)

    #3: - In form #5; attempt to access the global. Always NULL.

    I'm developing in a VM on a Windows system, so I can't give you a URL to have a proper look.


    Posted 9 years ago on Monday November 14, 2011 | Permalink
  5. GOT IT !!!!

    It turns out that Wordpress (on Windows) doesn't use nor want session !
    The solution is to insert an action hook to start a session at init time. Then session variables work !

    ARGH - is this a feature of all WordPress or just Windows ?

    I am well aware that stateless operation is safest and best. However I have a perfectly good reason here for wanting to persist a value across pages. Why is it so difficult and am I the only one ?

    Thanks anyway Chris.


    Posted 9 years ago on Monday November 14, 2011 | Permalink
  6. Probably Windows. I would test this in your production environment so you are not trying to solve problems that will not exist when your site goes live.

    Posted 9 years ago on Monday November 14, 2011 | Permalink