【问题标题】:wordpress dashboard display custom error message upon custom conditionwordpress 仪表板根据自定义条件显示自定义错误消息
【发布时间】:2014-03-20 07:15:46
【问题描述】:

在更新元信息之前,在 wordpress 仪表板的 edit-user.php 中,我正在检查一个条件,如果该条件失败,我想显示一条错误消息。我尝试使用更新的类来回显 div,还尝试了 WP admin_notices 钩子,但没有运气

 add_action( 'personal_options_update', 'save_extra_profile_fields' );
 add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );  

function save_extra_profile_fields( $user_id ) {
global $wpdb;        

    if(CONDITION TRUE) {
    update_usermeta( ........... );
    }
    else {
     WANT TO DISPLAY ERROR MESSAGE
     }
}

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    有一个用于验证用户额外字段的钩子。此钩子将在更新用户详细信息之前调用。

    你可以像这样显示错误信息:-

    add_action( 'user_profile_update_errors', 'validate_extra' );
    function validate_extra(&$errors, $update = null, &$user  = null)
    {
        if (!$_POST['YOUR_FIELD'])
        {
            $errors->add('YOUR_FIELD', "<strong>ERROR</strong>: YOUR ERROR MESSAGE.");
        }
    }
    
    add_action( 'personal_options_update', 'save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
    function save_extra_profile_fields( $user_id )
    {
        global $wpdb;
    
        update_usermeta( ........... );
    }
    

    【讨论】:

    • 你拯救了我的一天我一直在努力寻找这个钩子......请告诉我们如何获得这些钩子信息
    • 你可以找到任何 WordPress Action Hooks HereFilter Hooks Here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2011-07-29
    • 1970-01-01
    • 2016-09-19
    • 2021-12-01
    相关资源
    最近更新 更多