MyBB Community Forums

Full Version: How to retrieve pid (post id) with a plugin?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi.

This is a simple question, but I just can't figure it out.

I'd like to add a custom BBCode tag that needs the corresponding post id (pid) to work properly. Is there a way to get this id via a hook or something like that? I need to identify a post from within a parsed_message handler.

Thanks
Marc
It all depends on which page you want to use. You can probably use
function my_function(){
global $mybb;
$pid = $mybb->input['pid'];
}
But as I said, it depends on the page and which plugin handle you want to use. Can you give us more information?
I wrote a TOC (Table of Contents) BBCode Plugin to add a generated structure overview to a post.

Something like

Table of Contents

[toc]



[toc1]Overview[/toc1]

[toc2]Subitem[/toc2]

[toc3]An other subitem[/toc3]

[toc2]Test[/toc2]

[toc1]Introduction[/toc1]

would result in this output:

Table of Contents

1. Overview
1.1 Subitem
1.1.1 An other subitem
1.2 Test
2. Introduction



1. Overview (toc)

1.1 Subitem (toc)

1.1.1 An other subitem (toc)

1.2 Test (toc)

2. Introduction (toc)

The items in the generated TOC section are clickable and should move the browser's viewport to the corresponding part of the post. (Items are named anchors: #toc1, #toc1_1, #toc1_1_1 and so on.)

But what if there are two posts on the same thread page using [toc]? That's why I need a unique value to distinguish between these two posts. (I don't want to send the user to post 1 when he tried to reach a section in post 2.)

Therefor I'd like to add the post id to my anchors' names (e. g. #pid3_toc2_3).

I tried your above example but it always returns the same pid for every post on the page.

My handler should be parse_message (I'm fine with every handler returning the correct value) and my pages are default auto-generated thread pages.

I hope you know what I am looking for. My English is a little bit... unused. Wink
try
function my_function(){
$pid = $GLOBALS['post']['pid'];
or
$pid = $_GLOBALS['post']['pid'];
}
Thanks a lot!

By the way: Do you think there is a problem if I write something like this?

function myfunc()
{
    global $post;
    $pid = $post['pid'];
}

For some reason, I don't like $GLOBAL.

Thanks again
Marc
$post isn't defined as global in the postify function. So even if you defined it global....
Ignore all that. It might work yes.... lol.