0

I'm trying to write some files to the server when the settings for my plugin are saved. I'm using the Settings API and a class that looks like -

class AAPF_Admin {

  public function __construct() {

    add_action('admin_menu', array($this, 'aapf_options_page'));

    /* Register the settings when the options pages are initialized */
    if ( !empty($GLOBALS['pagenow']) and 
       ( 'options-general.php' === $GLOBALS['pagenow'] || 
         'options.php' === $GLOBALS['pagenow'] ) ) {
      add_action( 'admin_init', array($this, 'aapf_register_settings'));
    }

    /* This is what I'm unable to make work */
    /* I've tried  
       add_option_aapf
       add_option_aapf_options
       add_option_aapf-options
       add_option_aapf_main_settings
       add_option_aapf_field_sandbox
    */
    add_action( 'add_option_aapf_options', function( $a, $b ) {

      // write some files to the server...

    }, 10, 2 );

  }

  /* Adds the settings page for this form */
  public function aapf_options_page() {

    add_options_page( 
      'Page Title',                                 // Page Title
      'Menu Title',                                 // Menu Title
      'manage_options',                             // Capability
      'aapf-options',                               // Menu Slug
      array($this, 'aapf_options_page_html')        // Callback
    );

  }

  public function aapf_register_settings() {

    register_setting( 'aapf', 'aapf_options' ); // option group, option name

    add_settings_section(
      'aapf_main_settings',                       // id, used in form tag on admin screen
      __( 'Settings', 'aapf' ),                   // title or the section
      array($this, 'aapf_settings_callback'),        // callback
      'aapf'                                      // page to display on
    );

    // ... register settings fields ...

  }
}

If anyone has any suggestions I would be really grateful. Thanks!

Edit: I have seen this question and this question and they got me on the right track, but I'm still having this problem.

  • add_option_{$option} is firing inside add_option function, so I am guessing you have to add some options first with add_option function. – Laxmana Mar 6 '17 at 12:57
  • from my understanding register_setting adds options, and uses add_option to update them – rob-gordon Mar 6 '17 at 13:11
  • I don't see any calls to add_option inside register_setting function – Laxmana Mar 6 '17 at 13:24
  • as mentioned in the accepted answer to the 2nd question you site in your edited question, you should be hooking into update_option_aapf_options and not add_option_aapf_options. Using the Settings API, the function hooked to add_option_aapf_options will only be called the first time you save changes on your settings page. – Paul 'Sparrow Hawk' Biron Mar 6 '17 at 15:00

Your Answer

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

Browse other questions tagged or ask your own question.