How do I create an add-on that allows a user to click a link and call php code?

AndyB

Well-known member
I would like to create an add-on that allows an admin to change the date of a post. Under each post there will be a link the admin can click. A form will be displayed and when submitted the database is updated. I already have a stand alone php script to do this, but I would like to learn how to create a proper add-on.

Jake told me the initial steps.

1) You need to create a route so you have a location to request with the link.
2) You need a controller for that route to actually execute php code when that link is clicked.
3) Then add the link to the template.

So lets start with step #1, how do I create a route?
 
Hi Jeremy,

I would like to learn how to create an add-on. I looked at the add-on "Edit Post Date by Waindigo", but it's very complicated and I have no idea what does what. So I hope to be able to create this add-on with help from others.

So how do I define a route? Also will the XML file for the add-on define the route as well?
 
Last edited:
I assume to create a route this screen in the Admin CP is used.

pic001.webp

Route Prefix: tools
Route Type: Public
Route Class: I have no idea what goes here
Use class to build link: Only when data is provided
Add-on: Change Date
 
I assume to create a route this screen in the Admin CP is used.

View attachment 56813

Route Prefix: tools
Route Type: Public
Route Class: I have no idea what goes here
Use class to build link: Only when data is provided
Add-on: Change Date
As I said before. Create new route isn't nessasery, Why You don't extends XenForo_ControllerPublic_Post then you can using actionChangeDate. If you create new route you need create new ControllerPublic... Not good for your aim
So I hope to be able to create this add-on with help from others.
 
Hi Nobita.Kun,

Thank you for your assistance. I have no idea what "extends XenForo_ControllerPublic_Post" means.

What I would like to do is have a link located below the post. When the user clicks the link it will show them a few input fields and a submit button.
 
  • Display link you should using Template_Hook for display
  • Display form overlay in class link you must use: class="OverlayTrigger"
  • Want to change datetime of post you need post_id so extends XenForo_ControllerPublic_Post is better for that. You can find post_id by $postId = $this->_input->filterSingle('post_id', XenForo_Input::UINT); then to do something
  • When You extends that so link should be for using: {xen:link 'posts/whatever', $post}
 
  • Display link you should using Template_Hook for display
  • Display form overlay in class link you must use: class="OverlayTrigger"
  • Want to change datetime of post you need post_id so extends XenForo_ControllerPublic_Post is better for that. You can find post_id by $postId = $this->_input->filterSingle('post_id', XenForo_Input::UINT); then to do something
  • When You extends that so link should be for using: {xen:link 'posts/whatever', $post}

I'll start with the last bullet. Lets say I change whatever to "changedate". So when the user hovers over the link would it display something like:

http://www.mysite.com/forums/posts/123456/changedate
 
No. You need create new Event Listener here: {boardUrl}/admin.php?code-event-listeners/

Okay I'll do that in a second.

But first if I assume correctly, I need to create an index.php file here:

library/Andy/ChangeDate/index.php

PHP:
<?php

class Andy_ChangeDate_index
{
   public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
   {   
     if ($hookName == 'post_private_controls')
     {
       $contents = $contents . 'test';
     }
   }
}
 
Last edited:
Back
Top Bottom