I am writing a plugin and I’m currently implementing my admin menu using the wordpress settins API. As always with API’s, it facilitates programming to a certain degree, but it seems like I have an issue:
I need to wrap custom html around those sections. For example I want to wrap my form elements around a fieldset or I want to position logical units into two columns using CSS(float: right, float:left). In order to do so, I need to pack these sections into a container.
This is the API function that echo’s the sections.
function do_settings_sections( $page ) {
global $wp_settings_sections, $wp_settings_fields;
if ( ! isset( $wp_settings_sections[$page] ) )
return;
foreach ( (array) $wp_settings_sections[$page] as $section ) {
if ( $section['title'] )
echo "<h3>{$section['title']}</h3>n";
if ( $section['callback'] )
call_user_func( $section['callback'], $section );
if ( ! isset( $wp_settings_fields ) || !isset( $wp_settings_fields[$page] ) || !isset( $wp_settings_fields[$page][$section['id']] ) )
continue;
//MY WRAPPING NEEDS TO START HERE
echo '<table class="form-table">';
do_settings_fields( $page, $section['id'] );
echo '</table>';
//AND END HERE!
}
}
As far as I read the source correctly, there’s no way to wrap custom html around it (only before, with the add_settings_section( $id, $title, $callback, $page ) $callback parameter).
So how would you solve this problem? Should I just inject the appropriate containers into the the global $wp_settings_sections (With the ordinary hook I will write the div and then in the last field of each setting I can close the container with /div. But this is a very very ugly workaround and it find it repellent to say the least!
Should I abandon the idea of using the great settings API?
I’ve just run across this same issue myself on a project I am currently working on.
Basically, what I did was break my settings into groups so they can then be displayed as a separate section(s):
Hope this helps and makes sense to you 🙂
Good luck.
What works for me for just css styling or js scripts is using admin_enqueue_scripts:
And by using the
if ('bla bla bla' != $hook)
you simplyreturn
before any enqueueing if it’s not your plugin options page.$hook
appears to be what gets sent to the callback in theadd_action
call, and I retrieved it’s value for this specific plugin options page simply with addingprint_r($hook)
to the plugin options page for one load and grabbed it out of the browser.A less hackish way to do that would be by writing out to a file with a function like:
Which would be used:
And you’d avoid the ugly messages about the header already having been written.