Nistor Alexandru
Nistor Alexandru

Reputation: 5393

Wordpress Settings API two sections same page save error

Hi I am just learning to work with the settings API and I cant seem to get to sections on the same page to work.The first section works perfetly and saves the daya but the second displays an error on save :

Options Page not Found

This is the code for my entire options page:

<?php
/* ------------------------------------------------------------------------ *
 * REGISTER MENU AND SUBMENU
 * ------------------------------------------------------------------------ */
function thanathos_theme_menu()
{
  add_menu_page('Thanathos',
      'Thanathos',
      'administrator',
      'thanathos_menu_id',
      'thanathos_display'
  );
}

add_action('admin_menu', 'thanathos_theme_menu');

/*------------------------------------------------------------------------ *
* Social & Logo Options
* ------------------------------------------------------------------------ */
function thanathos_initialize_frontpage_options()
{
  if (false == get_option('thanathos_front_page_options')) {
    add_option('thanathos_front_page_options');
  }
  add_settings_section(
      'thanathos_front_page',
      '',
      'thanathos_front_page_section_callback',
      'thanathos_front_page_options'
  );
  add_settings_field(
      'logo_path',
      'Logo Path',
      'thanathos_logo_path_url_callback',
      'thanathos_front_page_options',
      'thanathos_front_page'
  );
  register_setting(
      'thanathos_front_page_options',
      'thanathos_front_page_options',
      'thanathos_front_page_options_sanitize'
  );
}

add_action('admin_init', 'thanathos_initialize_frontpage_options');
function thanathos_front_page_section_callback()
{
}

function thanathos_logo_path_url_callback()
{
  $options = get_option('thanathos_front_page_options');
  echo '<input type="text" id="logo" name="thanathos_front_page_options[logo_path]" value="' . $options['logo_path'] . '" />';
}

function thanathos_front_page_options_sanitize($input)
{
  $output = array();
  foreach ($input as $key => $val) {
    if (isset($input[$key])) {
      $output[$key] = strip_tags(stripslashes($input[$key]));
    }
  }
  return apply_filters('thanathos_front_page_options', $output, $input);
}

/* ------------------------------------------------------------------------ *
* Slider Options
* ------------------------------------------------------------------------ */
function thanathos_front_page_slider_options()
{
  if (false == get_option('thanathos_front_page_slider')) {
    add_option('thanathos_front_page_slider');
  }
  add_settings_section('thanathos_front_page_slider',
      '',
      'thanathos_front_page_slider_callback',
      'thanathos_display',
      'thanathos_front_page_slider'
  );
}

add_action('admin_init', 'thanathos_front_page_slider_options');
function thanathos_front_page_slider_callback()
{
}

/* ------------------------------------------------------------------------ *
 * Display on Thanathos Menu Page
 * ------------------------------------------------------------------------ */
function thanathos_display()
{
  ?>
    <style>
        fieldset {
            border: 1px solid #ddd;
            margin-top: 20px;
            padding-bottom: 20px;
        }

        legend {
            margin-left: 5px;
            padding: 0 5px;
            color: #2481C6;
            text-transform: uppercase;
        }

        p.submit {
            margin-left: 10px;
        }

        td input {
            width: 360px;
        }
    </style>
    <div class="wrap">
        <div id="icon-themes" class="icon32"></div>
        <h2>Thanathos General Options</h2>
      <?php settings_errors(); ?>
        <form method="post" action="options.php">
            <fieldset>
                <legend><strong>Logo and Social Options</strong></legend>
              <?php
              settings_fields('thanathos_front_page_options');
              do_settings_sections('thanathos_front_page_options');
              ?>
            </fieldset>
          <?php submit_button(); ?>
        </form>
        <form method="post" action="options.php">
            <fieldset>
                <legend><strong>Slider Options</strong></legend>
              <?php
              settings_fields('thanathos_front_page_slider');
              do_settings_sections('thanathos_front_page_slider');
              ?>
            </fieldset>
          <?php submit_button(); ?>
        </form>
    </div>
  <?php
}
?>

If this is not the corect approch of adding two sections to the same page then what is?

Upvotes: 0

Views: 617

Answers (1)

janw
janw

Reputation: 6662

Below is an excellent tutorial for the settings api. Altough a bit long if you want to do it as a whole.
In part 5 it explains why you can't have 2 settings_sections on one page.
The solution: use tabs.
This will help you with your problem.

http://wp.tutsplus.com/tutorials/the-complete-guide-to-the-wordpress-settings-api-part-1/

Upvotes: 2

Related Questions