1

The output of the settings API's function add_settings_field() is this:

<tr>
 <th scope="row">Your title</th>
 <td> Your field output </td>
</tr>

I know I can't change the way it's output, but I was wondering if there is a way I can put a display:none; on the <tr> with PHP? I don't want to do it with Javascript because then there is a flash of unstyled content.

Basically, I want a way to hide a row that's output via add_settings_field(), because I need it on the frontend - but later on in my flow.

1
  • Plenty of answers already but I don't think I understand. add_settings_field() doesn't return/output anything, so not sure what you are doing...
    – jimihenrik
    Jun 26, 2017 at 3:24

3 Answers 3

2

You can hide it using jQuery which will be inserted in the wp-admin header:

add_action( 'admin_head', 'wpse_239421_hide_section' );
function wpse_239421_hide_section() {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
        $('[scope=row]').closest('tr').hide();
        } );
    </script>
    <?php
}

This will remove the closet tr bases on the scope=row.

1
  • I know I can use jQuery, but as clearly stated in my question, I don't want to use Javascript because there's a flash. Until the document is ready, I can see the HTML and then all of a sudden, it disappears.
    – PoeHaH
    Sep 16, 2016 at 5:13
1

The sixth parameter ($args) of the add_settings_field function can contain a class value that will be added to the <tr>.

You can use this with the built-in admin css class .hidden

add_settings_field(
    'field_id',
    'Useless Field Label',
    'render_hidden_field_function',
    'some_settings_page',
    'a_section_slug',
    [
        'class' => 'hidden'
    ]
);
1
  • This would hide the input field, but not the entire tr including the field label.
    – EHerman
    Jan 20, 2019 at 14:33
0
    jQuery(document).ready(function(){
    jQuery('.my_add_settings_field_class').find('[scope=row]').hide();  
    });
2
  • Hi, thanks for your answer. Could you explain why do you think it will solve the problem? Dec 3, 2018 at 21:39
  • This only removes the first row.
    – welek
    Dec 10, 2018 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.