I have some pieces of code within functions.php that I could easly replace with much shorter functions, like, for example:

function register_sections() {
   add_settings_section('SOMETHING_settings', 'SOMETHING Settings', 'generate', 'SOMETHING_page' );
   add_settings_section('SOMETHING2_settings', 'SOMETHING2 Settings', 'generate', 'SOMETHING2_page' );
   add_settings_section('SOMETHING3_settings', 'SOMETHING3 Settings', 'generate', 'SOMETHING3_page' );
   add_settings_section('SOMETHING4_settings', 'SOMETHING4 Settings', 'generate', 'SOMETHING4_page' );

add_action('admin_init', 'register_sections');

I guess function replacing the code above will look like:

function settings_sections() {
   foreach($array as $section) {
       echo "add_settings_section('".$section."_settings','".$section." Settings','generate','".$section."_page' ) ;";    

But I'm not sure how to initalize this dynamically generated content? Of course add_action('admin_init', 'settings_sections'); echoes function's output on all admin pages instead of registering sections.

  • 1
    Interestingly, I've already linked (and copy-pasted) functioning code for this exact implementation, in your other question. I do basically exactly what @m0r7if3r used in his answer. Feb 10, 2012 at 17:13

1 Answer 1


Try this instead:

function settings_sections() {
    // array containing settings identifiers

    // loop through settings identifiers and generate settings sections
    foreach( $array as $v) {
            $v . '_settings',
            $v . ' Settings',
            $v . '_page'

echo outputs a string, you should just be calling add_settings_section() directly from inside the loop.

Also, NB: generate() is not the best name for a function, you should really go with something that's less likely to overlap with another function (prefix it with something)

  • Exactly right. Also: I'd give another +1 if I could, for the comment about proper function namespacing. A good practice is always to prefix anything in the public namespace, with theme-slug (or plugin-slug, as appropriate). Feb 10, 2012 at 17:11

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.