Denis Milosavljevic
Denis Milosavljevic

Reputation: 405

WordPress plugin development - adding action inside __construct() function

I am not sure how to add action properly within __() construct function...

Here is my code below:

class helloWorld{ 

    function __construct() {
        add_action( 'woocommerce_single_product_summary', array( $this, 'action_wwoocommerce_single_product_summary' ) );
    }


    function action_woocommerce_single_product_summary()   {
        return '<br /><h1>Hello World</h1>'; 
    }

}

$Hello_World = new helloWorld();

So, I am trying to output function action_woocommerce_single_product_summary() here -> action_wwoocommerce_single_product_summary but the problem is I dont know where to put int $priority in this case...

wp codex:

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )

Thanks in advance!!! Denis

Upvotes: 2

Views: 527

Answers (2)

barbocc
barbocc

Reputation: 189

I'm not sure but as folows from codex:

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )

if 2nd parameter $function_to_add is inside class then function add_class need a object. Object is $this parameter. $this parameter is for callback!

But 3rd and 4th parameter are just a variable that passed to add_action function.

In summary

//i think that this works fine
add_action( 'woocommerce_single_product_summary', array( $this, 'action_woocommerce_single_product_summary' ), 10, 1);

Upvotes: 1

Greg Burkett
Greg Burkett

Reputation: 1945

One small thing it may be... There's a typo in your add_action line, as you're calling "action_wwoocommerce_single_product_summary" (note the extra 'w' in woocommerce).

Upvotes: 1

Related Questions