How to Create a Settings Page for a WordPress Plugin

Create a new PHP file for your settings page. You can name it something like “plugin-settings.php”.

Add the necessary WordPress hooks to create the settings page. Here’s an example:

<?php
// Add a menu item for your settings page in the WordPress admin menu
add_action('admin_menu', 'my_plugin_add_settings_page');

function my_plugin_add_settings_page() {
add_options_page(
'My Plugin Settings', // Page title
'My Plugin', // Menu title
'manage_options', // Capability required to access the page
'my-plugin-settings', // Menu slug
'my_plugin_render_settings_page' // Callback function to render the page content
);
}

// Render the settings page content
function my_plugin_render_settings_page() {
?>
<div class="wrap">
<h1>My Plugin Settings</h1>
<form method="post" action="options.php">
<?php
// Output the settings fields
settings_fields('my_plugin_settings');
do_settings_sections('my-plugin-settings');
submit_button();
?>
</form>
</div>
<?php
}

// Register and define the settings fields and sections
add_action('admin_init', 'my_plugin_register_settings');

function my_plugin_register_settings() {
// Register a settings section
add_settings_section(
'my_plugin_general_settings', // Section ID
'General Settings', // Section title
'my_plugin_general_settings_callback', // Callback function to output section description (optional)
'my-plugin-settings' // Page slug
);

// Register a settings field
add_settings_field(
'my_plugin_text_field', // Field ID
'Text Field', // Field title
'my_plugin_text_field_callback', // Callback function to output field HTML
'my-plugin-settings', // Page slug
'my_plugin_general_settings', // Section ID
[
'label_for' => 'my_plugin_text_field',
] // Additional arguments (optional)
);

// Register the settings
register_setting(
'my_plugin_settings', // Settings group
'my_plugin_text_field' // Option name
);
}

// Callback function to output section description
function my_plugin_general_settings_callback() {
echo 'General settings for My Plugin';
}

// Callback function to output the text field HTML
function my_plugin_text_field_callback($args) {
$option = get_option('my_plugin_text_field');
echo '<input type="text" id="' . $args['label_for'] . '" name="' . $args['label_for'] . '" value="' . esc_attr($option) . '" />';
}

In the example above, the code registers a menu item under the “Settings” menu in the WordPress admin dashboard. It also defines a section and a text field for the settings page. The values of the settings fields are saved using the register_setting function and can be retrieved using the get_option function.

Save the PHP file and upload it to your plugin’s directory.

Activate your plugin in the WordPress admin dashboard.

After completing these steps, you should see a new menu item for your plugin under the “Settings” menu in the WordPress admin dashboard. Clicking on that menu item will take you to your plugin’s settings page, where you can define and modify the settings for your plugin.