wp设置和选项的请求方法api

Request method of wp setting and option api

我正在构建一个插件。为了呈现菜单页面,使用了选项 API 和设置 API。以下是 add_menu_page()

的回调函数
<?php
//Calback function for add menu page

function render_newsletter_admin() {
  if(!current_user_can( 'manage_options' )){
    return;
  }
  if($_SERVER["REQUEST_METHOD"]=="GET"){
   
    add_settings_error( 'newsletter', 'newsletter', __( 'Settings Saved', 'victory' ), 'success' );
    settings_errors(  );
  }
      ?>
    <div class="wrap">
      
        <form action="options.php" method="post">
            <?php
            // output security fields for the registered setting "wporg"
            settings_fields( 'newsletter' );
            // output setting sections and their fields
            // (sections are registered for "wporg", each field is registered to a specific section)
            do_settings_sections( 'newsletter' );
            // output save settings button
            submit_button( 'Save Settings' );
            ?>
        </form>
    </div>
    <?php
}

请注意,表单的方法属性设置为 POST 但要添加设置错误 $_SERVER["REQUEST_METHOD"]=="GET" 正在检查并且正在运行。不应该对照 POST 进行检查吗?

Shouldn't it be checked against POST?

它是根据 GET 而不是 POST 检查的,因为表单正在提交给 options.php(或更准确地说,wp-admin/options.php)并由其处理,并且在处理提交之后, WordPress 将 重定向 用户返回到提交的来源,即具有该表单的菜单页面。因此,请求方法将是 GET。

请求方法如何变化的说明(从GETPOST然后回到GET

  1. 用户登陆菜单页面(请求方式为GET)。
  2. 然后he/she提交了表格。
  3. 浏览器使用 POST 方法将表单提交给 options.php
  4. 现在options.php页面,请求方式为POST
  5. 并且在处理表单提交后,WordPress 将用户发送回 menu/form 页面。
  6. 现在在菜单页面上,请求方法是GET,因为上面的重定向使用了那个方法。

相关代码在wp-admin/options.php

  • 所以在 wp-admin/options.php 的顶部,它说...

    /**
     * Options Management Administration Screen.
     *
     * If accessed directly in a browser this page shows a list of all saved options
     * along with editable fields for their values. Serialized data is not supported
     * and there is no way to remove options via this page. It is not linked to from
     * anywhere else in the admin.
     *
     * This file is also the target of the forms in core and custom options pages
     * that use the Settings API. In this case it saves the new option values
     * and returns the user to their page of origin.
     *
     * @package WordPress
     * @subpackage Administration
     */
    
  • 以及第 339-341 行 (https://core.trac.wordpress.org/browser/tags/5.8/src/wp-admin/options.php#L339):

    // Redirect back to the settings page that was submitted.
    $goback = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
    wp_redirect( $goback );
    

并且如果您查看“complete example" in the plugin developer handbook, it's actually also checking against GET whereby add_settings_error() 仅在设置 $_GET['settings-updated'] 时调用:

// add error/update messages

// check if the user have submitted the settings
// WordPress will add the "settings-updated" $_GET parameter to the url
if ( isset( $_GET['settings-updated'] ) ) {
    // add settings saved message with the class of "updated"
    add_settings_error( 'wporg_messages', 'wporg_message', __( 'Settings Saved', 'wporg' ), 'updated' );
}

// show error/update messages
settings_errors( 'wporg_messages' );

此外,您可能希望在 if 之外调用 settings_errors()(如上),并将 newsletter 作为函数的第一个参数传递,即执行 settings_errors( 'newsletter' ).