• From what I read to add arguments to a function specified in add_action I need to do the following:

    add_action(‘wp_head’,’vclAddtoHead’,10,2);
    do_action(‘wp_head’,’somescripts’, ‘somemorescripts’);

    function vclAddtoHead($arg1,$arg2){
    echo $arg1;
    echo $arg2;
    }

    So this should specify that I want to add two arguments to vclAddtoHead() and that these two arguments which are specified here as ‘somescripts’ and ‘somemorescripts’ should be printed in the head tags when the page is built.

    However I keep getting the following error: Warning: Missing argument 2 for vclAddtoHead()

    Also the header section seems to be being written twice, both times incorrectly. Where am I going wrong?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You aren’t passing two arguments into your custom function vclAddtoHead(). It’s nothing to do with add_action. And why are you using a do_action? Try getting rid of it.

    Thread Starter s1ngular1ty

    (@s1ngular1ty)

    OK let me take a step back.

    I have defined my function vclAddtoHead as to take two arguments:

    function vclAddtoHead($arg1,$arg2){
    echo $arg1;
    echo $arg2;
    }

    I want this function to echo the arguments in the head tags. So thought I needed a function as follows:

    add_action(‘wp_head’,’vclAddtoHead’);

    But I need at some point to actually give my two arguments to my function. So how do I do this? I thought I had to modify add_action so that it specifies that my function takes two arguments, as follows:

    add_action(‘wp_head’,’vclAddtoHead’,10,2);

    Am I correct so far?

    If so what, now? Where do I actually specify what my two arguments are?

    In short how do I pass arguments to a function used in an add_action?

    The add_action is fine. Your function will be automatically called when wp_head runs. As for your variables – what are they meant to be?

    Thread Starter s1ngular1ty

    (@s1ngular1ty)

    Thanks for trying to help me esmi, I really appreciate it. But could you tell me how to pass arguments to the function given in the following statement.

    add_action(‘wp_head’,’vclAddtoHead’,10,2);

    The variables are just strings. I have simplified my code in the above examples, for ease of reading.

    The reason I thought I needed a do_action was due to the following link:

    https://www.ads-software.com/support/topic/passing-arguments-to-add_action?replies=8

    If ANYONE can tell me how to pass arguments with add_action I will be v gratefull.

    do_action() is called when you are creating your own custom action/hook. In the case of using add_action() and using an existing action (ie. ‘wp_head’) the arguments depend on the action you are using. In the case of wp_head there are none, so you’d add a function like:

    function my_wp_head_handler() {
        // your code to be executed when wp_head action is fired
        // ... perhaps vclAddtoHead('myarg1', 'myarg2');
    }
    
    add_action('wp_head', 'my_wp_head_handler');

    I’m not sure where the arguments are coming from, but you’ll need to get them depending on what you’re trying to do.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using arguments with add_action and do_action’ is closed to new replies.