• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/add settings error

提供: WordPress Codex 日本語版
< 関数リファレンス
2015年8月16日 (日) 13:03時点におけるGblsm (トーク | 投稿記録)による版 (見出しとリンク先のみ和訳。)

移動先: 案内検索

このページ「関数リファレンス/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.

使い方

<?php add_settings_error( $setting, $code, $message, $type ) ?>

パラメータ

$setting
string) (必須) Slug title of the setting to which this error applies.
初期値: なし
$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.
初期値: なし
$message
string) (必須) The formatted message text to display to the user (will be shown inside styled <div> and <p>).
初期値: なし
$type
string) (optional) 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'

戻り値

(void) 
This function does not return a value.

用例

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
    );

}

参考

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

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

変更履歴

Since: 3.0

ソースファイル

add_settings_error() is located in wp-admin/includes/template.php.

Settings API: register_setting(), unregister_setting(), add_settings_field(), add_settings_section(), add_settings_error(), get_settings_errors(), settings_errors(), settings_fields(), do_settings_sections(), do_settings_fields()



関数リファレンステンプレートタグ目次もご覧ください。


最新英語版: WordPress Codex » Function_Reference/add_settings_error最新版との差分