Skip to content

Instantly share code, notes, and snippets.

@digvijayad
Last active August 13, 2023 16:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save digvijayad/946e60b8c8c3a7984800608143e625f0 to your computer and use it in GitHub Desktop.
Save digvijayad/946e60b8c8c3a7984800608143e625f0 to your computer and use it in GitHub Desktop.
WordPress add settings field dynamically using associative arrays
<?php
add_action('admin_menu', 'myTheme_admin');
function myTheme_admin() {
/* Base Menu */
add_menu_page(
'myTheme settings', // Admin page title
'myTheme settings', // Admin menu label
'manage_options',
'myTheme-general-options', // Admin slug
'myTheme_general_index'); // Display Page
}
add_action('admin_init', 'myTheme_options');
function myTheme_options() {
$settings = array(
'setting_1_id' => array(
'title'=>'First Box Settings',
'page'=>'first_box_option',
'fields'=> array(
array(
'id'=> 'box_first_title',
'title'=>'Title',
'callback'=> 'text_callback'
),
array(
'id'=> 'box_first_desc',
'title'=>'Description',
'callback'=> 'textarea_callback'
),
array(
'id'=> 'box_first_link',
'title'=>'Link',
'callback'=> 'text_callback'
),
)
),
'setting_2_id' => array(
'title'=>'Second Box Settings',
'page'=>'second_box_option',
'fields'=> array(
array(
'id'=> 'box_second_title',
'title'=>'Title',
'callback'=> 'text_callback'
),
array(
'id'=> 'box_second_desc',
'title'=>'Description',
'callback'=> 'textarea_callback'
),
array(
'id'=> 'box_second_link',
'title'=>'Link',
'callback'=> 'text_callback'
),
)
),
'setting_3_id' => array(
'title'=>'Third Box Settings',
'page'=>'third_box_option',
'fields'=> array(
array(
'id'=> 'box_third_title',
'title'=>'Title',
'callback'=> 'text_callback'
),
array(
'id'=> 'box_third_desc',
'title'=>'Description',
'callback'=> 'textarea_callback'
),
array(
'id'=> 'box_third_link',
'title'=>'Link',
'callback'=> 'text_callback'
),
)
),
);
foreach( $settings as $id => $values){
add_settings_section(
$id, // ID used to identify this section and with which to register options
$values['title'],
'boxes_front_page_callback', // Callback used to render the description of the section
$values['page'] // Page on which to add this section of options
);
foreach ($values['fields'] as $field) {
// code...
add_settings_field(
$field['id'], // ID used to identify the field throughout the theme
$field['title'], // The label to the left of the option interface element
$field['callback'],
$values['page'], // The page on which this option will be added
$id, // ID of the section
array(
$values['page'], //option name
$field['title'] //id
)
);
}
register_setting($values['page'], $values['page']);
} // end of foreach
} // end of myTheme_options()
/*********************************
* Callbacks
**********************************/
function boxes_front_page_callback() {
echo '<p>Lorem ipsum</p>';
}
function text_callback($args) {
$options = get_option($args[0]);
echo '<input type="text" class="regular-text" id="' . $args[1] . '" name="'. $args[0] .'[' . $args[1] . ']" value="' . $options['' . $args[1] . ''] . '"></input>';
}
function textarea_callback($args) {
$options = get_option($args[0]);
echo '<textarea rows="8" cols="50" class="large-text" id="' . $args[1] . '" name="'. $args[0] .'[' . $args[1] . ']">' . $options['' . $args[1] . ''] . '</textarea>';
}
/***************************************
* Display Page
***************************************/
function myTheme_general_index() {
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2>myTheme Settings</h2>
<?php settings_errors(); ?>
<?php
$active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'boks_pierwszy';
?>
<h2 class="nav-tab-wrapper">
<a href="?page=myTheme-general-options&tab=first" class="nav-tab <?php echo $active_tab == 'first' ? 'nav-tab-active' : ''; ?>">First Tab</a>
<a href="?page=myTheme-general-options&tab=second" class="nav-tab <?php echo $active_tab == 'second' ? 'nav-tab-active' : ''; ?>">Second Tab</a>
<a href="?page=myTheme-general-options&tab=third" class="nav-tab <?php echo $active_tab == 'third' ? 'nav-tab-active' : ''; ?>">Third Tab</a>
</h2>
<form method="post" action="options.php">
<?php
if( $active_tab == 'first' ) {
settings_fields( 'first_box_option' );
do_settings_sections( 'first_box_option' );
} else if( $active_tab == 'second' ) {
settings_fields( 'second_box_option' );
do_settings_sections( 'second_box_option' );
} else if( $active_tab == 'third' ) {
settings_fields( 'third_box_option' );
do_settings_sections( 'third_box_option' );
}
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment