WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to pass CLASS FUNCTION with arguments to add_action? (5 posts)

  1. Hire Ninjas
    Member
    Posted 1 year ago #

    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! :)

  2. catacaustic
    Member
    Posted 1 year ago #

    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.

  3. Hire Ninjas
    Member
    Posted 1 year ago #

    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

  4. catacaustic
    Member
    Posted 1 year ago #

    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.

  5. Hire Ninjas
    Member
    Posted 1 year ago #

    ok, thanks :)

Topic Closed

This topic has been closed to new replies.

About this Topic