作为背景,并回答我自己的问题(经过一些研究后)<form> 选项屏幕中的标记通常使用类似以下代码生成:
<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() 函数是Options API的一部分,负责创建add\\u settings\\u fields语句中定义的选项设置字段。(参见
https://developer.wordpress.org/reference/functions/do_settings_sections/ ).
这个do_settings_section() 函数最终调用do_settings_fields() 输出(渲染)实际<input> 通过回调指定的语句add_settings_field(). (参见https://developer.wordpress.org/reference/functions/do_settings_fields/ ).
如果我们看一下do_settings_field, 我们将找到输出每个设置字段(到表中)的语句:
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>\';
我们可以在这里看到
<th> 字段代码
$title (定义见
add_settings_field 设置字段的定义)。
因此,以您想要的格式输出没有表格的设置字段的唯一方法是绕过do_settings_section() 在<form> 使用自己的代码,例如使用自己的my_render_fields() 设置窗体中的函数:
<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>
请注意
settings_field() 还将在表单中包含设置页面上需要的“nonce”。(没有nonce可能会导致设置屏幕上出现其他错误。)
希望这对其他人有帮助。深入研究WP函数的源代码有时会让您深入了解问题。(我希望有一个过滤器可以绕过场的渲染,但没有这样的运气。但是创建自己的场渲染函数并不难。)