0

I'm writing a plugin "Paid Post" with some options. I made a settings page for my plugin, and it displays OK, but when I'm pressing "save changes" button, the option I have do not change, it just stays "on". Reading "Settings API" page didn't help, neither did exploring other plugins. Sections of code for admin options:

// Admin options

function PPOptionsPage() {
  add_settings_section("section_main", __("Main options", 'paid-posts'), '', "paid-posts");

  add_settings_field(
    "pp_display_excerpt",
    __("Display post excerpt on a single page before paywall text", 'paid-posts' ),
    "PPCheckbox",
    "paid-posts",
    "section_main",
    array( 'label_for' => 'pp_display_excerpt')
  );
  register_setting("paid-posts", "pp_display_excerpt");
}

function PPCheckbox($options)
{
  echo '<input type="checkbox" class="code" name="'. $options['label_for'] .'" id="'. $options['label_for'] .'" value="1"' . checked(1, get_option($options['label_for']), false) . '>';
}

add_action("admin_init", "PPOptionsPage");

function PPAdminMenu() {
  add_options_page( 
    __('Paid Post options', 'paid-posts'),
    __('Paid Post', 'paid-posts'),
    'manage_options',
    'paid-posts',
    'PPAdminOptions'
  );
}

function PPAdminOptions() {
  if ( !current_user_can( 'manage_options' ) )  {
    wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
  }
  ?>
      <div class="wrap">
         <h1><?php echo get_admin_page_title(); ?></h1>

         <form method="post" action="">
            <?php
               settings_fields("section_main");

               do_settings_sections("paid-posts");

               submit_button();
            ?>
         </form>
      </div>
   <?php
}

add_action( 'admin_menu', 'PPAdminMenu' );
3

You need to register the proper settings fields in your PPAdminOptions() and also point the form action to options.php:

function PPAdminOptions() {
if ( !current_user_can( 'manage_options' ) )  {
    wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
?>
<div class="wrap">
    <h1><?php echo get_admin_page_title(); ?></h1>

    <form method="post" action="options.php">
        <?php
        settings_fields("paid-posts");

        do_settings_sections("paid-posts");

        submit_button();
        ?>
    </form>
</div>
<?php
}

I tested this on my local machine, and it saved the checkbox state.

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.