Skip to content

Instantly share code, notes, and snippets.

@johnregan3
Last active May 25, 2023 07:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save johnregan3/6076713 to your computer and use it in GitHub Desktop.
Save johnregan3/6076713 to your computer and use it in GitHub Desktop.
A method for dynamically generating Settings Fields in WordPress through the use of an anonymous function assigned to a variable variable. This could be expanded to generate different input types (hidden, text, textarea, and maybe a select), and to dynamically generate Setting Sections and Section Callbacks.
<?php
/*
* Dynamically Generated Checkboxes in a WordPress Settings Page
*
* This file is fully functional and ready to be used in a plugin or theme to generate this Settings Page
*
*/
Class Pizza_Shop {
//Set up Toppings Array to be used to generate checkboxes
static $toppings = array(
"crust" => array(
"thick", "thin",
),
"meat" => array(
"beef", "pepperoni", "sausage", "canadian bacon",
),
"veggies" => array(
"mushrooms", "onions", "olives", "tomatoes",
),
"other" => array(
"extra cheese", "extra sauce", "anchovies",
),
);
//This method takes two parts of the settings field name and assembles them. (e.g., "meat_beef")
public static function input_setup( $choice_title ){
$options = get_option('pizza_shop_settings');
$value = (isset( $options[$choice_title] ) ? $options[$choice_title] : '');
return $value;
}
}
//Add Menu Page
add_action( 'admin_menu', 'pizza_shop_add_toppings_menu' );
function pizza_shop_add_toppings_menu() {
add_menu_page( __( 'Pizza Shop Settings' ), __( 'Pizza Shop' ), 'administrator', basename(__FILE__), 'pizza_shop_general_settings' );
}
//Register Setting and add Sections
function pizza_shop_render_fields() {
register_setting( 'pizza_shop_settings', 'pizza_shop_settings', 'validate_pizza_shop_settings' );
add_settings_section('meat_section', __( 'Meat Toppings', 'pizza_shop' ), 'meat_section_cb', __FILE__ );
add_settings_section('veggies_section', __( 'Veggie Toppings', 'pizza_shop' ), 'veggies_section_cb', __FILE__ );
add_settings_section('other_section', __( 'Other Toppings', 'pizza_shop' ), 'other_section_cb', __FILE__ );
}
add_action( 'admin_init', 'pizza_shop_render_fields' );
//The basic markup for the Settings Page.
function pizza_shop_general_settings() {
?>
<div id="pizza_shop-settings-wrap" class="wrap">
<div class="icon32" id="icon-options-general">
<br />
</div>
<?php _e( '<h2>Pizza Shop Toppings</h2>', 'pizza_shop'); ?>
<form method="post" action="options.php" enctype="multipart/form-data">
<?php settings_fields( 'pizza_shop_settings' ); ?>
<?php do_settings_sections( __FILE__ ); ?>
<p class="submit">
<input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e( 'Send Me A Pizza!' ); ?>" />
</p>
</form>
</div>
<?php
}
//Here's where all the action happens
function generate_toppings_checkboxes() {
//Fetch Toppings Array
$toppings_array = Pizza_Shop::$toppings;
//Now we're going to extract each combination of type and choice (e.g., "meat" and "Beef")
foreach ( $toppings_array as $type => $choices ) {
$toppings_type = $type;
//foreach choice, we will generate a checkbox and an add_settings_field function
foreach ( $choices as $choice ) {
//start the string that we'll be using for ids and names. (e.g., meat_beef)
$choice_title = strtolower( $type . '_' . $choice );
/* Here is where the action happens.
* I'm using (unneeded) brackets here to help visualize what's happening.
* This variable variable basically generates "$meat_beef"
*
* Then, we're using this variable to hold an anonymous function.
* "use" is basically how we pass variables to an anonymous function.
*/
${$choice_title} = function() use ( $type, $choice, $choice_title ) {
$options_value = Pizza_Shop::input_setup( $choice_title );
echo "<input type='checkbox' id='pizza_shop_settings[". esc_attr( $choice_title ) . "]' name='pizza_shop_settings[" . esc_attr( $choice_title ) . "]' value='1' " . checked( 1, isset( $options_value ) ? $options_value : 0, false ) ." />";
};
/*Capitalize the choice ("beef") so we can present it as the field title
*(the second parameter in add_settings_field)
*/
$cap_choice_title = ucwords( $choice );
/* The third parameter of add_settings_field takes a function name,
* we're using the dynamically generated variable to reference the anonymous function above.
*/
add_settings_field( $choice_title, esc_html__( $cap_choice_title, 'pizza_shop' ), ${$choice_title}, __FILE__, $type . '_section' );
}
}
}
add_action( 'admin_init', 'generate_toppings_checkboxes' );
//Functions required by the add_settings_section()s above
function meat_section_cb() {}
function veggies_section_cb() {}
function other_section_cb() {}
//Chincy validation function
function validate_pizza_shop_settings( $input ) {
return $input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment