【问题标题】:Show custom error messages from wp plugin form submit显示来自 wp 插件表单提交的自定义错误消息
【发布时间】:2021-01-26 20:58:14
【问题描述】:

我正在开发一个 WordPress 插件,并且对 WordPress 来说非常新我正在寻找一种方法来根据 wp-admin 中插件设置页面中的一些逻辑显示自定义错误消息

我在 wp-admin 中显示了这个表单

<form method="post" action="options.php" autocomplete="off">
                    <?php
                    // This prints out all hidden setting fields
                    settings_fields('iskills_pros_and_cons');

                    // output setting sections based on tab selections
                    if ($active_tab == 'global') {
                        do_settings_sections('iskills_pros_and_cons_default');
                    } else if ($active_tab == 'heading') {
                        do_settings_sections('iskills_pros_and_cons_heading');
                    } else if ($active_tab == 'section') {
                        do_settings_sections('iskills_pros_and_cons_body');
                    } else if ($active_tab == 'button') {
                        do_settings_sections('iskills_pros_and_cons_button');
                    } else {
                        do_settings_sections('iskills_pros_and_cons_icons');
                    }

                    submit_button();
                    ?>
                </form>

我想在表单提交时检查活动选项卡,如果活动选项卡是全局的,则执行一些检查,如果这些检查失败,那么我想将错误发送回同一设置页面。谁能指导我在 WordPress 中实现这一目标的更好方法是什么?在这方面的任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我使用 sanitize 方法进行自定义验证,如果失败返回错误

    public function sanitize($input) {
            if(true) {
                // There is an error
                add_settings_error( 'general', 'settings_updated', __( "Message" ), 'error' );
                set_transient( 'settings_errors', get_settings_errors(), 30 );
                // Redirect back to the settings page that was submitted.
                $goback = add_query_arg( 'settings-updated', 'true', wp_get_referer() );
                wp_redirect( $goback );
                exit;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-29
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多