Create a Custom Settings Page in WordPress

Create a Custom Settings Page in WordPress

WordPress setting page is a dedicated page within the WordPress admin area that allows users to manage and configure various settings for their website. It provides a user-friendly interface for making changes to the website’s features, appearance, and functionality. The setting page enables users to control various aspects of their website such as security settings, theme options, plugin options, and other settings. It is a central hub for all the configurations and options for a WordPress website, making it easier for users to manage their website without having to modify any code. The custom settings page in WordPress helps users to keep their website up to date, secure, and functional, without having to rely on technical expertise.

How To add a setting page in WordPress for “My Custom Page,” follow these steps:

  1. Create a new PHP file for the setting page: In the WordPress theme folder, create a new PHP file. You can name it “my-custom-page-settings.php.”
  2. Add the WordPress action to the new PHP file: In the new file, add the following code to register the setting page with WordPress:
    add_action('admin_menu', 'my_custom_page_settings');
    function my_custom_page_settings() {
      add_options_page(
        'My Custom Page Settings', 
        'My Custom Page', 
        'manage_options', 
        'my-custom-page-settings', 
        'my_custom_page_settings_render'
      );
    }
    

     

  3. Create the setting page content: In the new file, add the following function to render the content of the setting page:
    function my_custom_page_settings_render() {
      ?>
      <div class="wrap">
        <h1>My Custom Page Settings</h1>
        <form method="post" action="options.php">
          <?php
          settings_fields('my_custom_page_settings_group');
          do_settings_sections('my-custom-page-settings');
          submit_button();
          ?>
        </form>
      </div>
      <?php
    }
    
  4. Create the form fields: In the same file, add the following code to create the form fields for the setting page:
    add_action('admin_init', 'my_custom_page_settings_fields');
    function my_custom_page_settings_fields() {
      register_setting('my_custom_page_settings_group', 'my_custom_page_title');
      add_settings_section(
        'my_custom_page_section', 
        'My Custom Page Settings', 
        'my_custom_page_section_render', 
        'my-custom-page-settings'
      );
      add_settings_field(
        'my_custom_page_title', 
        'Page Title', 
        'my_custom_page_title_render', 
        'my-custom-page-settings', 
        'my_custom_page_section'
      );
    }
    
  5. Render the form fields: In the same file, add the following code to render the form fields:
    function my_custom_page_section_render() {
      echo 'Enter the details for the custom page:';
    }
    
    function my_custom_page_title_render() {
      $options = get_option('my_custom_page_title');
      ?>
      <input type="text" name="my_custom_page_title" value="<?php echo esc_attr($options); ?>" />
      <?php
    }
    
  6. Load the setting page: Finally, include the new PHP file in your functions.php file or any other relevant file to load the setting page in the WordPress admin area.You have now successfully added a setting page in WordPress for “My Custom Page.” You can now access the setting page from the WordPress admin area and customize the page title.