1

Is there a way, using the Settings API, to disable the use of the cell of the table created for options?

The add_settings_field 2nd parameter is the $title attribute (string) and is required. This string is then placed to the left of the input area for the settings field. This 'indents' the input area.

I put the 'description' of the setting input area after the settings tag when rendered. Don't need any space to the left of the <input> tag.

What are the recommendations for removing that space to the left of the <input> tag, using the Settings API in the standard way? For instance, the Settings, Permalinks page has the <input> areas at the left edge.

Thanks.

1

As background, and to answer my own question (after some research), the <form> tag in an Options screen is usually generated with code similar to this:

<form action='options.php' method='post'>
<?php
settings_fields( 'pluginPage' );    // initializes all of the settings fields
do_settings_sections( 'pluginPage' );   // does the settings section; into a table
submit_button();    // creats the submit button
?>
</form>

The do_settings_section() function is part of the Options API, and is responsible for creating the option settings fields defined in add_settings_fields statements. (See https://developer.wordpress.org/reference/functions/do_settings_sections/ ).

The do_settings_section() function eventually calls do_settings_fields() which outputs (renders) the actual <input> statements via the callback specified by add_settings_field(). (See https://developer.wordpress.org/reference/functions/do_settings_fields/ ).

If we look at the code for do_settings_field, we will find statements that output each settings field (into a table - sigh):

echo "<tr{$class}>";

if ( ! empty( $field['args']['label_for'] ) ) {
    echo '<th scope="row"><label for="' . esc_attr( $field['args']['label_for'] ) . '">' . $field['title'] . '</label></th>';
} else {
    echo '<th scope="row">' . $field['title'] . '</th>';
}
echo '<td>';
call_user_func($field['callback'], $field['args']);
echo '</td>';
echo '</tr>';

And we can see here the <th> code for the field $title (defined in the add_settings_field definition of the settings field).

So, the only way to output settings fields without a table, in the format you want, is to bypass the do_settings_section() in the <form> with your own code, such as using your own my_render_fields() function in the settings form:

<form action='options.php' method='post'>
<?php
settings_fields( 'pluginPage' );    // initializes all of the settings fields
my_render_fields();     // render fields without do_settings so no table codes
submit_button();    // creates the submit button
?>
</form>

Note that settings_field() will also include the 'nonce' in the form, which is needed on Settings pages. (Not having a nonce can cause other errors on settings screens.)

Hope this helps others. Digging into the source code of WP functions sometimes gives you insights into problems. (I was hoping for a filter that would bypass the rendering of the field, but no such luck. But creating your own field rendering function is not hard.)

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.