add_settings_error()注册添加一个设置错误提示信息

目录

描述

Register a settings error to be displayed to the user.

Part of the Settings API. Use this to show messages to users about settings validation problems, missing settings or anything else.

Settings errors should be added inside the $sanitize_callback function defined in register_setting() for a given setting to give feedback about the submission.

By default messages will show immediately after the submission that generated the error. Additional calls to settings_errors() can be used to show errors even when the settings page is first accessed.

add_settings_error() 描述

用法

<?php add_settings_error$setting$code$message$type ?>

add_settings_error() 用法

参数

$setting

(string) (必填) Slug title of the setting to which this error applies.

默认值: None

$code

(string) (必填) Slug-name to identify the error. Used as part of 'id' attribute in HTML output. A prefix of 'setting-error-' will be added to the string in $code and assigned to the 'id' attribute of the outermost <div> for this error.

默认值: None

$message

(string) (必填) The formatted message text to display to the user (will be shown inside styled <div> and <p>).

默认值: None

$type

(string) (可选) The type of message it is. $type will be add an HTML class of the same name of the outermost <div>. To add multiple classes separate them with a space.

  • error
  • updated

默认值: 'error'

add_settings_error() 参数

返回值

(void) 

This function does not return a value.

add_settings_error() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
function change( $data ) {

    $message = null;
    $type = null;

    if ( null != $data ) {

        if ( false === get_option( 'myOption' ) ) {

            add_option( 'myOption', $data );
            $type = 'updated';
            $message = __( 'Successfully saved', 'my-text-domain' );

        } else {

            update_option( 'myOption', $data );
            $type = 'updated';
            $message = __( 'Successfully updated', 'my-text-domain' );

        }

    } else {

        $type = 'error';
        $message = __( 'Data can not be empty', 'my-text-domain' );

    }

    add_settings_error(
        'myUniqueIdentifyer',
        esc_attr( 'settings_updated' ),
        $message,
        $type
    );

}

add_settings_error() 示例

注意

The problem only occurs when I use add_menu_page() or add_submenu_page(). The validation errors are not shown.

A simple work around is to add settings_errors() to my options page, which will make the notification box show... However, seems like a dirty fix

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<h2>The heading of my settings page</h2>
<?php settings_errors(); ?>

add_settings_error() 注意

历史

添加于 版本: 3.0

add_settings_error() 历史

源文件

add_settings_error() 函数的代码位于 wp-admin/includes/template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Register a settings error to be displayed to the user
 *
 * Part of the Settings API. Use this to show messages to users about settings validation
 * problems, missing settings or anything else.
 *
 * Settings errors should be added inside the $sanitize_callback function defined in
 * register_setting() for a given setting to give feedback about the submission.
 *
 * By default messages will show immediately after the submission that generated the error.
 * Additional calls to settings_errors() can be used to show errors even when the settings
 * page is first accessed.
 *
 * @since 3.0.0
 *
 * @global array $wp_settings_errors Storage array of errors registered during this pageload
 *
 * @param string $setting Slug title of the setting to which this error applies
 * @param string $code    Slug-name to identify the error. Used as part of 'id' attribute in HTML output.
 * @param string $message The formatted message text to display to the user (will be shown inside styled
 *                        `
` and `

` tags). * @param string $type Optional. Message type, controls HTML class. Accepts 'error' or 'updated'. * Default 'error'. */ function add_settings_error( $setting, $code, $message, $type = 'error' ) { global $wp_settings_errors; $wp_settings_errors[] = array( 'setting' => $setting, 'code' => $code, 'message' => $message, 'type' => $type ); }

add_settings_error() 源文件

相关