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

Template Modification: PHP Callback does not work

Discussion in 'XenForo Development Discussions' started by rellek, Sep 4, 2013.

  1. rellek

    rellek Well-Known Member

    Let's just assume you want to clear the template 'header' with "XenForo rocks" using a PHP callback in a template modification... I think this doesn't work.

    Here's what I entered:
    Template: header
    Modification Key: removeheader
    Description: test
    Search Type: PHP Callback
    Find: /.+/is
    Replace: rellek_TMSTest::myMethod

    and there's a file, ./library/rellek/TMSTest.php which looks like this:

    class rellek_TMSTest {

         public function 
    myMethod(array $args) {

    'XenForo rocks';


    This won't work.

    It doesn't match anything when I click "Test Modification" and has the status 0/0/1 in the TM list.

    If I change PHP callback to Regular Expression, It shows me that it matches everything and replaces this with my callback name.

    Question is, is this a bug or am I doing something wrong?
  2. Mike

    Mike XenForo Developer Staff Member

    Your method needs to be static.
    rellek likes this.
  3. Dan

    Dan Well-Known Member

    the method needs to begin with

    • get
    • is has
    • render
    • view
    • return
    • print
    • show
    • display
    and it needs to be static
  4. Mike

    Mike XenForo Developer Staff Member

    Template modifications don't requite the method prefix (xen:callback does).
    Dan likes this.
  5. rellek

    rellek Well-Known Member

    D'oh! Thanks.

    But why is it callable then? I mean, I did look into the XF source code and it does check if it is callable...?
  6. Jeremy

    Jeremy Well-Known Member

    Why is it callable after you make it static? Because the system doesn't create an instance of your class to run your code, so it needs to be static to allow that to work.
  7. xf_phantom

    xf_phantom Well-Known Member

    Marcus likes this.
  8. rellek

    rellek Well-Known Member

    No it was callable (according to that function is_callable) before I made it static. I saw in the source code that XF checks if a method is callable and throws an error if not. But @xf_phantom answered this, it seems to be a PHP flaw then.

Share This Page