‘global’ not working when passing variable from plugin file to template

Your code has the correct syntax, but for globals to work they have to be run in the same PHP execution (i.e. the same page load), and you’ll need to set that global before you read it. Globals are completely separate to each page load.

You either have a case where the code in the template is running before the plugin has run to set the global, or you’re trying to access the variable in a completely different page load to when you set it, so it won’t be available. If your ‘template’ file isn’t part of WordPress at all, WordPress will never run to set the global.

So you have a few options:

  • If your template isn’t part of WordPress, load WordPress from your template so that the plugin runs and sets the global. See @breadwild’s comment.
  • Put the makeAppeal function in a library somewhere so that you can call it from both inside and outside of WordPress to get the appeal string value from the template. If you don’t load WordPress, this relies on that function not calling WordPress functionality.

I’m assuming that the $appeal value changes depending on a particular case or event or something the user is looking at, so you could also look at persisting that value in the WP database. For example, if the appeal value is associated with a WP custom post type, you can use update_post_meta and get_post_meta to persist those values in the database. Again, that would mean you’d need to load WordPress from your template in order to have access to the get_post_meta function, and ensure that it’s set before you try to access it.

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş