Posts: 262
Threads: 39
Joined: Mar 2019
Reputation:
20
I've been able to complete this using an IDENTICAL code for profiles (minus obvious necessary changes) but I cannot figure out why the postbit one is not working.
Hook:
$plugins->add_hook('postbit', 'widowccglobals_posts');
Code:
function widowccglobals_posts($post)
{
global $mybb, $db, $lang, $templates, $character_postbit, $member_postbit;
$query = $db->simple_select("settings", "*", "name='wcc_icgroups'");
$wcc_icgroups = $db->fetch_field($query, "value");
$wcc_icgroups_array = explode(',', $wcc_icgroups);
if(in_array($post['usergroup'], $wcc_icgroups_array))
{
eval("\$character_postbit = \"".$templates->get("postbit_character")."\";");
} else {
eval("\$member_postbit = \"".$templates->get("postbit_member")."\";");
}
}
The templates are not being called at all...
Posts: 37,500
Threads: 399
Joined: Apr 2008
Reputation:
773
2023-01-13, 07:01 PM
(This post was last modified: 2023-01-13, 07:04 PM by Matt. Edited 3 times in total.)
What is it not doing that you're expecting it to do?
I'm not sure why you're querying the settings table here - not only will this be an unnecessary performance issue as it will be doing a query for every post, but the value of the settings is available in $mybb->settings. You should never need to directly query the settings table for anything.
You'll also want to re-set $character_postbit and $member_postbit to '' at the start, otherwise it'll retain the value from the previous call as it's a global variable, maybe that's what the issue is if it's showing the wrong thing (but you've not given any details on what's wrong so unsure).
Posts: 37,500
Threads: 399
Joined: Apr 2008
Reputation:
773
Oh, I just saw that the template isn't being called... where are you using the variables $character_postbit and $member_postbit? Are they in another template? They won't output anything unless they're used in another template.
Posts: 262
Threads: 39
Joined: Mar 2019
Reputation:
20
2023-01-13, 07:12 PM
(This post was last modified: 2023-01-13, 07:12 PM by Taylor M.)
(2023-01-13, 07:01 PM)Matt Wrote: What is it not doing that you're expecting it to do?
the postbit_character and postbit_member are not showing anything when the variables $character_postbit and $member_postbit are referenced in the postbit template.
(2023-01-13, 07:01 PM)Matt Wrote: I'm not sure why you're querying the settings table here - not only will this be an unnecessary performance issue as it will be doing a query for every post, but the value of the settings is available in $mybb->settings. You should never need to directly query the settings table for anything.
Do you mean $mybb or $db? If I remove $db from the code the whole page goes white.
(2023-01-13, 07:01 PM)Matt Wrote: You'll also want to re-set $character_postbit and $member_postbit to '' at the start, otherwise it'll retain the value from the previous call as it's a global variable, maybe that's what the issue is if it's showing the wrong thing (but you've not given any details on what's wrong so unsure).
Updated(?):
function widowccglobals_posts($post)
{
global $mybb, $db, $templates, $character_postbit, $member_postbit;
$query = $db->simple_select("settings", "*", "name='wcc_icgroups'");
$wcc_icgroups = $db->fetch_field($query, "value");
$wcc_icgroups_array = explode(',', $wcc_icgroups);
if(in_array($post['usergroup'], $wcc_icgroups_array))
{
$character_postbit = '';
eval("\$character_postbit = \"".$templates->get("postbit_character")."\";");
} else {
$member_postbit = '';
eval("\$member_postbit = \"".$templates->get("postbit_member")."\";");
}
}
Posts: 37,500
Threads: 399
Joined: Apr 2008
Reputation:
773
2023-01-13, 07:15 PM
(This post was last modified: 2023-01-13, 07:15 PM by Matt.)
The variable need to be reset outside of any conditions. Also you need to drop the database logic entirely, you don't need to query anything, you just need to use $mybb->settings['wcc_icgroups']
function widowccglobals_posts($post)
{
global $mybb, $templates, $character_postbit, $member_postbit;
$character_postbit = $member_postbit = '';
$wcc_icgroups_array = explode(',', $mybb->settings['wcc_icgroups']);
if(in_array($post['usergroup'], $wcc_icgroups_array))
{
eval("\$character_postbit = \"".$templates->get("postbit_character")."\";");
} else {
eval("\$member_postbit = \"".$templates->get("postbit_member")."\";");
}
}
Posts: 262
Threads: 39
Joined: Mar 2019
Reputation:
20
(2023-01-13, 07:15 PM)Matt Wrote: The variable need to be reset outside of any conditions. Also you need to drop the database logic entirely, you don't need to query anything, you just need to use $mybb->settings['wcc_icgroups']
LOL sorry, I've been staring at this screen so long that I read more into something than necessary. I posted before I realized what you meant there.
Also, now I see what you mean by the '' for each. I've updated it to reflect that but its still not calling the templates D:
Posts: 37,500
Threads: 399
Joined: Apr 2008
Reputation:
773
2023-01-13, 07:25 PM
(This post was last modified: 2023-01-13, 07:25 PM by Matt. Edited 1 time in total.)
Try changing it to set values to $post, should be able to receive $post by reference (so &$post in the function definition);
function widowccglobals_posts(&$post)
{
global $mybb, $templates;
$post['character_postbit'] = $post['member_postbit'] = '';
$wcc_icgroups_array = explode(',', $mybb->settings['wcc_icgroups']);
if (in_array($post['usergroup'], $wcc_icgroups_array)) {
eval("\$post['character_postbit'] = \"".$templates->get("postbit_character")."\";");
} else {
eval("\$post['member_postbit'] = \"".$templates->get("postbit_member")."\";");
}
}
Then use $post['character_postbit'] and $post['member_postbit'] in the template. If that doesn't work, sounds like the template modifications aren't applied to the right template set or something.
Posts: 462
Threads: 19
Joined: Jul 2008
Reputation:
64
Have you tried passing the $post variable by reference?
function widowccglobals_posts(&$post)
Posts: 262
Threads: 39
Joined: Mar 2019
Reputation:
20
(2023-01-13, 07:25 PM)Matt Wrote: Try changing it to set values to $post, should be able to receive $post by reference (so &$post in the function definition);
function widowccglobals_posts(&$post)
{
global $mybb, $templates;
$post['character_postbit'] = $post['member_postbit'] = '';
$wcc_icgroups_array = explode(',', $mybb->settings['wcc_icgroups']);
if (in_array($post['usergroup'], $wcc_icgroups_array)) {
eval("\$post['character_postbit'] = \"".$templates->get("postbit_character")."\";");
} else {
eval("\$post['member_postbit'] = \"".$templates->get("postbit_member")."\";");
}
}
Then use $post['character_postbit'] and $post['member_postbit'] in the template. If that doesn't work, sounds like the template modifications aren't applied to the right template set or something.
Works like a charm!!!
(2023-01-13, 07:25 PM)doylecc Wrote: Have you tried passing the $post variable by reference?
function widowccglobals_posts(&$post)
Also whats the different between using &$post vs $post?
Posts: 37,500
Threads: 399
Joined: Apr 2008
Reputation:
773
2023-01-13, 07:39 PM
(This post was last modified: 2023-01-13, 07:40 PM by Matt. Edited 1 time in total.)
Passing by reference means you pass the actual variable in to the function, so modifications to it will affect the variable outside, whereas by default you only pass through the value of a variable.
So normally:
function foo($bar)
{
$bar = 2;
}
$var = 1;
foo($var);
echo $var; // will echo 1
By reference:
function foo(&$bar)
{
$bar = 2;
}
$var = 1;
foo($var);
echo $var; // will echo 2
So with the new hook, you're modifying the actual $post variable itself. If you didn't have the & there and made changes to $post inside the function, the changes wouldn't exist outside the function.
|