Support » Plugins » Hacks » How to pass CLASS FUNCTION with arguments to add_action?

  • Resolved Hire Ninjas

    (@szmeku)


    This works:

    add_action('hook', array($this, 'someFunction'))

    These not:

    add_action('hook', array($this, 'someFunction'), 'argument')
    add_action('hook', array($this, 'someFunction'), $priority, 'argument')

    Plz help me! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • catacaustic

    (@catacaustic)

    very awesome

    http://codex.wordpress.org/Function_Reference/add_action

    You need to read that correctly. What you are trying to do is not correct. Your first example has thearguments in the wrong order so won’t work. Your second example almost has it right, but the final argument for $argument is the number of additional arguments that the function will accept, not the values that you want to pass in. The problem is that the arguments that are given are not ones that you can define.

    What you’d have ot od is get the values set up beforehand, and then read them in when the function is called. There’s a few ways of doing that, but seeing as how you’re uisng an OO approach, I’d suggest setting them up as values of your class, and using them when they are needed. That is by far the simplest way to do it.

    what do you mean by “seeing as how you’re using an OO approach”, that i’m not using reference to $this?

    for my question i’ve found answer here
    http://wordpress.stackexchange.com/questions/45901/passing-a-parameter-to-filter-and-action-functions

    and yeah that’s true i didn’t read this carefully, my fault 🙂 http://codex.wordpress.org/Function_Reference/add_action

    catacaustic

    (@catacaustic)

    very awesome

    what do you mean by “seeing as how you’re using an OO approach”, that i’m not using reference to $this?

    All I meant there is that you’re uisng classes and objects and not procedural code. It’s a good thing. 🙂

    That question/anser is exactly what I meant, and it is a good exampel of what would suit your situation.

    ok, thanks 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to pass CLASS FUNCTION with arguments to add_action?’ is closed to new replies.