Wordpress Action Hook inside Classes

37 Views Asked by At

The WordPress plugin is functioning correctly, and within the functions.php file, I aim to establish tabs using arguments and hooks. These tabs should consist of both a tab menu and tab content. However, after initiating the arguments, the classes should include add_action for the menu and add_action for the content. I believe I have followed the proper procedures, but I am not receiving any values in the do_action function. Could you please advise on where I might be encountering issues within the code?

function tabRun() {
    global $tabs;
    
    $tabs = array(
        array(
            'tab_name' => 'menu1',
            'tab_content' => 'content1'
        ),
        array(
            'tab_name' => 'menu2',
            'tab_content' => 'content2'
        ),
        // Dodajte više tabova po potrebi
    );
}

add_action('create_tab', 'tabRun');

class MyTabs {
    public function add_tabs() {
        global $tabs;

        foreach ($tabs as $tab) {
            add_action('mytabs_menu', function () use ($tab) {
                echo '<div id="tab-menu-' . esc_html($tab['tab_name']) . '" class="tab-menu" >' . esc_html($tab['tab_name']) . '</div>';
            });

            add_action('mytabs_content', function () use ($tab) {
                echo '<div id="tab-content-' . esc_html($tab['tab_name']) . '" class="tab-content">' . esc_html($tab['tab_content']) . '</div>';
            });
        }
    }
}

if (class_exists('MyTabs')) {
    global $my_tabs;
    $my_tabs = new MyTabs();
    $my_tabs->add_tabs(); // Dodajte ovu liniju da biste aktivirali dodavanje tabova
}

do_action('create_tab'); // Dodajte ovu liniju za poziv funkcije tabRun()
do_action('mytabs_menu'); // Dodajte ovu liniju za prikaz tabova u WordPress temi
do_action('mytabs_content'); // Dodajte ovu liniju za prikaz tabova u WordPress temi

0

There are 0 best solutions below