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
    Commented Jun 26, 2017 at 3:24

4 Answers 4

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
    Commented 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
    Commented 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? Commented Dec 3, 2018 at 21:39
  • This only removes the first row.
    – welek
    Commented Dec 10, 2018 at 7:59
0
add_settings_field(
    'myprefix_setting-id',
    'This is the setting title',
    'myprefix_setting_callback_function',
    'general',
    'default'',
     array('class' => 'hidden')
);

That should/must do the job. Just add the extra arguments "class" used when outputting the field.

2
  • 1
    Hi - if you want to take ownership of this with your new account, see the merging accounts help. Editing it won't do that.
    – Rup
    Commented Aug 15, 2022 at 16:02
  • 1
    Your answer looks identical to already given.
    – nmr
    Commented Aug 16, 2022 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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