MyBB Community Forums

Full Version: Global Plugin Variable Works Everywhere BUT Postbit?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've got a plugin where I've hooked into global_start for all variables I need globally.

In the plugin where I declare these variables using the global_start hook this is how they are fetched.
$global_icgroups_query = $db->simple_select("settings", "*", "name='icvsooc_icgroups'");
 $global_icgroups_fetch = $db->fetch_field($global_icgroups_query, "value");
 $global_icgroups = explode(',', $global_icgroups_fetch);

These variables work literally everywhere but the postbits and I'm confused why? I'm using this in combination with the php in templates/template conditionals plugin.

Example in welcomeblock:
<if in_array($mybb->user['usergroup'], $global_icgroups) then>stuff here</if>

Example in member profile:
<if in_array($memprofile['usergroup'], $global_icgroups) then>stuff here</if>

Example in postbit: (this one does not work)
<if in_array($post['usergroup'], $global_icgroups) then>test</if>
^ error thrown: in_array() expects parameter 2 to be array, null given

also tried the below which throws no errors but also doesn't work
<if in_array($post['usergroup'], array($mybb->settings['icvsooc_icgroups'])) then>test</if>
where $mybb->settings['icvsooc_icgroups'] outputs = 2,9,10,12

Not sure why its not able to fetch the information to be used in an array with postbits?
(2023-09-22, 08:24 PM)Taylor M Wrote: [ -> ]These variables work literally everywhere but the postbits and I'm confused why?

It's because the postbit template is rendered within a function - build_postbit() - and your variables aren't declared global within that function.

You can get around this by referencing them via the $GLOBALS array, e.g., reference as $GLOBALS['global_icgroups'] rather than as $global_icgroups.

(2023-09-22, 08:24 PM)Taylor M Wrote: [ -> ]also tried the below which throws no errors but also doesn't work
<if in_array($post['usergroup'], array($mybb->settings['icvsooc_icgroups'])) then>test</if>
where $mybb->settings['icvsooc_icgroups'] outputs = 2,9,10,12

Yep, array($mybb->settings['icvsooc_icgroups']) will create an array with one entry, the string '2,9,10,12'.

To create an array with entries 2, 9, 10, and 12 from that string, you'd need to explode it on commas, i.e., explode(',', mybb->settings['icvsooc_icgroups']).