Dynamically create callback functions in the loop in wordpress settings API

I created a WordPress setting page where you suppose to add some sub setting pages where code will be the same for each new page and that works fine.

Than I loop through sections and settings fields and concatenate name of the subpage and strings in order to create custom properties.

Read More

When I get to loop through callbacks Im having a problem. Since changing the names of functions inside the loop is not possible in php I wonder how could I do that?

  function initialize(){ 
        $myList = get_option('listOfMaps');
        $myList = explode(",", $myList);

        if(count($myList)>1){
          for ($x = 0; $x < count($myList)-1; $x++) {

          $pagename = str_replace(" ","",$myList[$x]);


          add_settings_section(
                    "googleMapsSection",        
                    "Google Maps Options Section",
                    $pagename."_google_maps_section",       
                    "maps".$pagename                    
                    );
   .....

   }
   add_action("admin_init","initialize");

That part does not work.

$myList = get_option('listOfMaps');
$myList = explode(",", $myList);

if(count($myList)>1){
    for ($x = 0; $x < count($myList)-1; $x++) {

       $pagename = str_replace(" ","",$myList[$x]);
       function $pagename."_google_maps_section"(){
                echo "Here you can adjust manually Google map options";
       }
    }
}

Thank you.

Related posts