1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is there a function to get a thread URL based on $thread_id input?

Discussion in 'XenForo Development Discussions' started by AndyB, Sep 26, 2013.

  1. AndyB

    AndyB Well-Known Member

    I'm creating an add-on in which I need to create a URL based on the $thread_id. Is there a function I can call that returns a link with the appropriate URL?

    I would also need a similar xF function for posts. I input the $post_id and the function returns the URL.

    Thank you.
  2. Jeremy

    Jeremy Well-Known Member

    {xen:link} or XenForo_Link allow you to retrieve URLs.
  3. AndyB

    AndyB Well-Known Member

    Hi Jeremy,

    Sorry I should have been more clear, I'm looking for a function to use in the PHP side of the add-on.
  4. Jeremy

    Jeremy Well-Known Member

    Look into XenForo_Link, you will find all the functions you need there.
  5. AndyB

    AndyB Well-Known Member

    Looks like the following is used to redirect:

          // regular redirect
    return $this->responseRedirect(
    How would I use XenForo_Link::buildPublicLink('threads', $thread) so that I get back a full URL in the form of:


    The following does not work.

    $thread = '1234';
    $var = XenForo_Link::buildPublicLink('threads', $thread);
  6. Jeremy

    Jeremy Well-Known Member

    You need to pass it as an array.
  7. AndyB

    AndyB Well-Known Member

    I've tried several different ways to express the array, but not having any luck.

    For example the following does not work.

    $thread = array('thread' => $thread_id);
  8. Jeremy

    Jeremy Well-Known Member

    You cannot just arbitrarily name your data when dealing with pre-defined XenForo functions. When building a link, it uses an ID and expects data named appropriately. Look into XenForo_Route_Prefix_Threads for the actual building of the threads route.
  9. Marcus

    Marcus Well-Known Member

    This might work:

    $thread['thread_id'] = '1234';
    $threadLink= XenForo_Link::buildPublicLink('threads', $thread);
  10. KenSmith

    KenSmith Active Member

    Andy, here's some more detail...

    The magic sauce for link-building resides in the Route_Prefix class for any given route.
    As Jeremy pointed out, for threads, that class is XenForo_Route_Prefix_Threads.
    Look in library/XenForo/Route/Prefix/Threads.php and you'll find a function buildLink.

    The most important line is the final one:
        return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'thread_id', 'title') . $postHash;
    Notice the 'thread_id' and 'title' names. Whatever array you pass will need those two keys to create an appropriate link.

    Most commonly, if you have an entire thread loaded into $thread already, you could get the link this way:
      XenForo_Link::buildPublicLink('threads', $thread)
    If you don't already have an array loaded with those fields, you could do this:
      XenForo_Link::buildPublicLink('threads',array('thread_id' => $threadId, 'title' => $title));
    Either way, you'll get back a link that looks like mysite.com/threads/thread-title.thread-id/

    I mainly go into all this because you'll probably want to add a buildLink into your Route_Prefix_ShowDelete class as well.
    The version in most of my route prefixes is a simple one-liner like this:
      public function buildLink($originalPrefix, $outputPrefix, $action, $extension, $data, array &$extraParams)
         return XenForo_Link::buildBasicLinkWithIntegerParam($outputPrefix, $action, $extension, $data, 'tourn_id', 'event_name');
    In that example, I use a tournament Id and the event's name to build a pretty link into my 'tournaments' route.
    The key is that if you want to add a single ID type parameter into your route's URLs, this tells it which field that represents,
    and additionally, which field can be used to build the SEO-friendly version that includes more than just the numeric ID.
    Make sense?
    rour and AndyB like this.
  11. Chris D

    Chris D XenForo Developer Staff Member

    Last edited: Sep 26, 2013
    AndyB, Jeremy and KenSmith like this.
  12. AndyB

    AndyB Well-Known Member

    Thank you, Marcus, Ken and Chris.

    The quoted example only partially works, I get the following:


    I wonder why the http://xenforo.com/community/ part on the link is not included?
  13. Daniel Hood

    Daniel Hood Well-Known Member

    change ('threads',.... to ('full:threads',...
    AndyB likes this.
  14. AndyB

    AndyB Well-Known Member

    That works perfect! Thank you, Daniel I really appreciate the help.
    Daniel Hood likes this.
  15. Jarvis

    Jarvis Member

    Sorry to bring this back.

    I have a similar case here:

    // redirect after post
    return $this->responseRedirect(
    XenForo_Link::buildPublicLink('threads', array('thread_id' => $threadID)),
    With the following function in my route _prefix
    public function buildLink($originalPrefix$outputPrefix$action$extension$data, array &$extraParams)
    Any idea what I'm doing wrong here? I don't get any redirect after a successful post, however the XenForo_Phrase does display correctly.

Share This Page