• Resolved RBX

    (@rbx)


    I’m sorry, this is more of a PHP language question, but I hope I’ll get help.

    I have a settings panel in which I have 12 similar functions, which just replace month name at 2-3 places. I didn’t want to define 12 callback functions, so I tried this and haven’t been able to get it to work.

    function display_festivals_fields() {
    	add_settings_section("section", "All Settings", null, "manage-festivals");
    
    	$months = array(
    		'january','february','march','april','may','june','july','august','september','october','november','december'
    	);
    
    	foreach($months as $month) {
    		add_settings_field("festivals_{$month}", ucwords($month) . " Festivals", function() use ($month) { echo "<textarea type='text' name=\"{$month}_festivals\" id=\"{$month}_festivals\" value='".get_option("{$month}_festivals")."' ></textarea>";}, "manage-festivals", "section");
    		register_setting("section", "festivals_{$month}");
    	}
    }
Viewing 1 replies (of 1 total)
  • Thread Starter RBX

    (@rbx)

    It was a silly mistake. I jumbled up option name and forgot that textarea doesn’t use value attribute.

    function display_festivals_fields() {
    	add_settings_section( "section", "All Settings", null, "manage-festivals" );
    
    	$months = [
    		'january',
    		'february',
    		'march',
    		'april',
    		'may',
    		'june',
    		'july',
    		'august',
    		'september',
    		'october',
    		'november',
    		'december',
    	];
    
    	foreach ( $months as $month ) {
    		add_settings_field(
    			"festivals_{$month}",
    			ucwords( $month ) . " Festivals",
    			function () use ( $month ) { echo "<textarea type='text' name=\"festivals_{$month}\" id=\"festivals_{$month}\">" . get_option( "festivals_{$month}" ) . "</textarea>"; },
    			"manage-festivals",
    			"section"
    		);
    		register_setting( "section", "festivals_{$month}" );
    	}
    }
Viewing 1 replies (of 1 total)

The topic ‘Easier process to define multiple similar callback functions’ is closed to new replies.