Custom variable not working in hook
#1
I have ran into an issue, and I haven't been able to resolve it myself. A quick search shows code similar to my own, but for some reason it just doesn't want to work for me. It's dead simple too.

I have a function that hooks into postbit. I have tried making this a custom variable, and also tried making it a key within the array. None of it wants to work.

This is my code:
$plugins->add_hook('postbit', 'add_to_postbit');

function add_to_postbit(&$post)
{

    $post['test'] = 'This is a TEST variable.';

}


The in the template I'm doing {$post['test']}, but it just doesn't output anything.

I have tried making it a custom variable $test in the add_to_postbit function, and also global'ing it inside that function, but it doesn't work either with the output {$test}.

Any ideas as to what I'm doing wrong?
[Image: sig.png]
Reply
#2
In which template are you adding it? The postbit hook placement in inc/functions_post.php is inconvenient, it's added after many postbit_ templates get eval'd. So you have to get around it by inserting something like <!--replaceme--> to that template and then using str_replace() to change it to your code.
Reply
#3
(02-04-2015, 09:29 PM)Destroy666 Wrote: In which template are you adding it? The postbit hook placement in inc/functions_post.php is inconvenient, it's added after many postbit_ templates get eval'd. So you have to get around it by inserting something like <!--replaceme--> to that template and then using str_replace() to change it to your code.

Would you be able to give an example of that in action?

Would it be something along the lines of this?

function add_to_postbit(&$post)
{

    global $post, $postbit, $templates;

    eval("\$postbit = \"".$templates->get("postbit")."\";");

    $postbit = str_replace('<!-- test -->', 'Testing 1,2,3', $postbit);
    
    return $postbit;
}
[Image: sig.png]
Reply
#4
(02-04-2015, 10:01 PM)Stewartiee Wrote: Would it be something along the lines of this?

No. So you're adding it to the postbit template, yes? Shouldn't be a problem then. Can't tell more without seeing the exact code.
Reply
#5
(02-04-2015, 10:11 PM)Destroy666 Wrote:
(02-04-2015, 10:01 PM)Stewartiee Wrote: Would it be something along the lines of this?

No. So you're adding it to the postbit template, yes? Shouldn't be a problem then. Can't tell more without seeing the exact code.

No, not the postbit it is the postbit_author_user where I'm trying to add the variable to. I just assumed I'd have to recreate the postbit template in that last code sample.
[Image: sig.png]
Reply
#6
Well, I asked earlier to which template you're adding it for a reason..

Anyways, you need something like this then:
function add_to_postbit(&$post)
{
    $post['user_details'] = str_replace('<!-- test -->', 'Testing 1,2,3', $post['user_details']);
}
Reply
#7
(02-04-2015, 10:27 PM)Destroy666 Wrote: Well, I asked earlier to which template you're adding it for a reason..

Anyways, you need something like this then:
function add_to_postbit(&$post)
{
    $post['user_details'] = str_replace('<!-- test -->', 'Testing 1,2,3', $post['user_details']);
}

Thank you for your help Smile
[Image: sig.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)