1

I've created a plugin where the settings are configured on different tabs. Each tab saves its settings to a different field in wp_options. The HTML for each tab is separated out into its own file so that there is minimal mixing of PHP and HTML.

I'd like to add additional fields to an existing tab, but I want to save these particular settings as a separate entry in wp_options. I know this is possible using add_settings_field and specifying the HTML in the callback function, but I would like to continue to keep the HTML separate from the PHP.

Is it possible to be able to do both?

Thx.

[UPDATED]

partials/book-review-admin-advanced.php:

<form action="options.php" method="post">
  <?php
    @settings_fields( 'advanced_options' );
    @do_settings_fields( 'advanced_options' );

    // This doesn't work:
    // @settings_fields( 'general_options' );
    // @do_settings_fields( 'general_options' );
    // Neither does adding a separate form for general_options.
  ?>
  <table class="form-table">
    <tbody>
      <tr>
        <th scope="row">
          <label for="book_review_api_key">
            <?php esc_html_e( 'Google API Key', $this->plugin_name ); ?>:
          </label>
        </th>
        <td>
          <input id="book_review_api_key" class="regular-text text-input" type="text"
        name="book_review_advanced[book_review_api_key]"
        value="<?php echo esc_attr( $advanced['book_review_api_key'] ); ?>">
        </td>
      </tr>
      <tr>
        <th scope="row">
          <label for="book_review_test">
            <?php esc_html_e( 'Test Field', $this->plugin_name ); ?>:
          </label>
        </th>
        <td>
          <!-- How can I save both book_review_advanced and book_review_general? -->
          <input id="book_review_test" class="regular-text text-input" type="text"
        name="book_review_general[test]">
        </td>
      </tr>
    </tbody>
  </table>


  <?php @submit_button(); ?>
</form>

class-book-review-admin.php:

public function init_menu() {
  register_setting( 'advanced_options', 'book_review_advanced', array( $this, 'sanitize_advanced' ) );
  register_setting( 'general_options', 'book_review_general', array( $this, 'sanitize_general' ) );
}

public function sanitize_advanced( $input = array() ) {
  $output = array();

  // Do some sanitization.

  return $output;
}

public function sanitize_general( $input = array() ) {
  $output = array();

  // Do some sanitization.

  return $output;
}

public function render_tabbed_content() {
  if ( isset ( $_GET['tab'] ) ) {
    $active_tab = $_GET['tab'];
  }
  else {
    $active_tab = 'advanced';
  }

  if ( $active_tab == 'advanced' ) {
    $advanced = get_option( 'book_review_advanced' );

    include_once( 'partials/book-review-admin-advanced.php' );
  }

  // Handle other tabs.
}
  • Could you post a simplified version of the code you are currently using? I can see what you are trying to do, but having some actual code would help. – Jan Beck Nov 25 '15 at 5:20
  • Thanks, I added some relevant snippets. Hopefully it makes things clearer. Cheers. – donnapep Nov 25 '15 at 22:28
1

Settings API is (also) about the modularity of the settings, settings can be removed (never heard of anyone do it but it is possible) or amended (my favorite technique right now).

When you separate the HTML and PHP you are breaking the modularity so maybe the setting API should just not be used by you.

As with many other things you can probably hack it to work with using all kinds of filters, but the question is why to use it at all if it is against your preferred development practice.

  • Ugh. The front-end developer in me cringes at mixing content and behaviour. However, I value my time more than this particular conviction, so I guess I'll have to suck it up if there's no relatively painless way of doing this. Thx. – donnapep Nov 26 '15 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.