Search code examples
phpwordpress

conditional in array add_settings_section


I am trying to add a conditional in the before_section array param of the add_settings_section, but, it just prints out a specific part.

This is the example of the function:

add_settings_section(
    'team_section_main',
    '',
    '',
    'team_searchbox',
    array(
        'before_section' => '<div class="tab-content" id="tab_main" style="'.$_GET['tab']=="tab_main"?"":"display:none;".'">',
        'after_section' => '</div>'
    )
);
add_settings_section(
    'team_section_destination',
    '',
    '',
    'team_searchbox',
    array(
        'before_section' => '<div class="tab-content" id="tab_destination" style="'.$_GET['tab']=="tab_destination"?"":"display:none;".'">',
        'after_section' => '</div>'
    )
);

I have several tabs and depending on the tab selected, I want a div element to be hidden or shown.

I seem to do it incorrectly as it just outputs display:none;>. So what do I miss? Most likely a typo.


Solution

  • Ok, I have found a workaround. Answering here, to add the code I updated. I am not using the style attribute anymore, I am just adding a class if I need to hide a specific tab.

    $tab_main = $_GET['tab'] === 'tab_main' ? '' : 'team-hide';
    $tab_dest = $_GET['tab'] === 'tab_destination' ? '' : 'team-hide';
    add_settings_section(
        'team_section_main',
        '',
        '',
        'team_searchbox',
        array(
            'before_section' => '<div class="tab-content '.$tab_main.'">',
            'after_section' => '</div>'
        )
    );
    add_settings_section(
        'team_section_destination',
        '',
        '',
        'team_searchbox',
        array(
            'before_section' => '<div class="tab-content '.$tab_dest.'">',
            'after_section' => '</div>'
        )
    );
    

    And then just using css on the team-hide class. This does the trick.