设置API问题:提交后未保存更改

时间:2018-09-26 作者:Sly

我正在写一个插件“付费帖子”,里面有一些选项。我为我的插件创建了一个设置页面,它显示OK,但当我按下“保存更改”按钮时,我的选项没有更改,它只会保持“打开”。阅读“设置API”页面并没有帮助,探索其他插件也没有帮助。管理选项的代码部分:

// 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\' );

1 个回复
最合适的回答,由SO网友:Hans 整理而成

您需要在PPAdminOptions() 并将窗体操作指向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
}
我在本地机器上测试了这个,它保存了复选框状态。

结束