plugins – WordPress Custom Hook with Class method

Question

The code described below works normally outside of wordpress, but in wordpress I get “warning expects parameter 1 to be valid callback function mycars ($ mycarsclass) not found”. If I remove $ mycarsclass and leave only add_node, it works normally. How do I enable this for mycars ($ mycarsclass) to be valid? Is this possible in wordpress?

<?php
class mycarsclass {
  public $car1;
  public $car2;
function add_node($args) {
    echo '<div class="demo">I lake ' . $args['car1'] . 'and' . $args['car2'] . '</div>';
}
}
$mycarsclass = new mycarsclass();

function mycars($mycarsclass){
     
    $args = array(
    'car1' => 'Volvo',  
    'car2' => 'Toyota'
    ); 
    
    $mycarsclass->add_node($args);  
}

add_action('mycarsaction','mycars($mycarsclass)');
do_action('mycarsaction');
?>

0
deki0 3 weeks 2021-11-07T12:21:56-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse