Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active February 10, 2022 20:10
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tommcfarlin/04782209b0822ce2bf1f to your computer and use it in GitHub Desktop.
Save tommcfarlin/04782209b0822ce2bf1f to your computer and use it in GitHub Desktop.
[WordPress] An example for how to properly add multiple sections to a WordPress options page. These gists demonstrate what *not* to do, and what *to* do.
<?php
add_action( 'admin_init', 'acme_admin_init_one' );
function acme_admin_init_one() {
add_settings_section(
'acme-settings-section-one', // String for use in the 'id' attribute of tags
'Acme Settings Part One', // Title of the section
'display_acme_settings_message', // Function that fills the section with the desired content
'acme-options-page' // The menu page on which to display this section. Should match $menu_slug.
);
add_settings_field(
'acme-input-field', // String for the 'id' attribute tags
'Acme Input Field', // Title of the field
'render_acme_input_field', // Function that fills the field with the desired inputs as part of the larger form.
'acme-options-page', // The menu page on which to display this field
'acme-settings-section-one' // The section of the settings page in which to show the box
);
register_setting(
'acme-settings', // A settings group name.
'acme-input-field' // The name of an option to save.
);
}
function display_acme_settings_message() {
echo "This displays the settings message.";
}
function render_acme_input_field() {
$input = get_option( 'acme-input-field' );
echo '<input type="text" id="acme-input-field" name="acme-input-field" value="' . $input . '" />';
}
<?php
add_action( 'admin_init', 'acme_admin_init_two' );
function acme_admin_init_two() {
add_settings_section(
'acme-settings-section-two',
'Acme Settings Part Two',
'display_another_acme_settings_message',
'acme-options-page'
);
add_settings_field(
'acme-input-field-one',
'Acme Input Field Two',
'render_acme_input_field_two',
'acme-options-page',
'acme-settings-section-two'
);
register_setting(
'acme-settings-two',
'acme-input-field-two'
);
}
function display_another_acme_settings_message() {
echo "This displays the second settings message.";
}
function render_acme_input_field_two() {
$input = get_option( 'acme-input-field-two' );
echo '<input type="text" id="acme-input-field-two" name="acme-input-field-two" value="' . $input . '" />';
}
<?php
add_action( 'admin_menu', 'add_acme_options_page' );
function add_acme_options_page() {
add_options_page(
'Acme Options', // The text to be displayed in the title tag
'Acme Options', // The text to be used for the menu
'manage_options', // The capability required to display this menu
'acme-options-page', // The unique slug name to refer to this menu
'display_acme_options_page' // The function tooutput the page content
);
}
<?php
add_action( 'admin_menu', 'add_acme_options_page' );
function add_acme_options_page() {
add_options_page(
'Acme Options',
'Acme Options',
'manage_options',
'acme-options-page',
'display_acme_options_page'
);
}
function display_acme_options_page() {
echo '<h2>Acme Options</h2>';
echo '<form method="post" action="options.php">';
do_settings_sections( 'acme-options-page' );
settings_fields( 'acme-settings' );
submit_button();
echo '</form>';
}
add_action( 'admin_init', 'acme_admin_init_one' );
function acme_admin_init_one() {
add_settings_section(
'acme-settings-section-one',
'Acme Settings Part One',
'display_acme_settings_message',
'acme-options-page'
);
add_settings_field(
'acme-input-field',
'Acme Input Field',
'render_acme_input_field',
'acme-options-page',
'acme-settings-section-one'
);
register_setting(
'acme-settings',
'acme-input-field'
);
}
function display_acme_settings_message() {
echo "This displays the settings message.";
}
function render_acme_input_field() {
$input = get_option( 'acme-input-field' );
echo '<input type="text" id="acme-input-field" name="acme-input-field" value="' . $input . '" />';
}
add_action( 'admin_init', 'acme_admin_init_two' );
function acme_admin_init_two() {
add_settings_section(
'acme-settings-section-two',
'Acme Settings Part Two',
'display_another_acme_settings_message',
'acme-options-page'
);
add_settings_field(
'acme-input-field-two',
'Acme Input Field Two',
'render_acme_input_field_two',
'acme-options-page',
'acme-settings-section-two'
);
register_setting(
'acme-settings',
'acme-input-field-two'
);
}
function display_another_acme_settings_message() {
echo "This displays the second settings message.";
}
function render_acme_input_field_two() {
$input = get_option( 'acme-input-field-two' );
echo '<input type="text" id="acme-input-field-two" name="acme-input-field-two" value="' . $input . '" />';
}
<?php
function display_acme_options_page() {
echo '<h2>Acme Options</h2>';
echo '<form method="post" action="options.php">';
do_settings_sections( 'acme-options-page' );
settings_fields( 'acme-settings' );
settings_fields( 'acme-settings-two' );
submit_button();
echo '</form>';
}
<?php
function display_acme_options_page() {
echo '<h2>Acme Options</h2>';
echo '<form method="post" action="options.php">';
do_settings_sections( 'acme-options-page' );
settings_fields( 'acme-settings' );
submit_button();
echo '</form>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment