Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用户仪表板中自定义字段的管理员通知_Php_Wordpress_Validation_Meta Boxes - Fatal编程技术网

Php 用户仪表板中自定义字段的管理员通知

Php 用户仪表板中自定义字段的管理员通知,php,wordpress,validation,meta-boxes,Php,Wordpress,Validation,Meta Boxes,在Wordpress中,我在用户仪表板中添加了一些字段。我将它们设为必需,但如果它们为空,我希望显示一条消息 以下是我所做工作的一个示例: function my_admin_notice() { ?> <div class="error"> <p><?php _e( 'Error!', 'user_street' ); ?></p> </div> <?php } function

在Wordpress中,我在用户仪表板中添加了一些字段。我将它们设为必需,但如果它们为空,我希望显示一条消息

以下是我所做工作的一个示例:

function my_admin_notice() { ?>
    <div class="error">
        <p><?php _e( 'Error!', 'user_street' ); ?></p>
    </div>
    <?php
}

function save_extra_user_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) ) { 

        return false;
    }

    if (!empty($_POST['user_street'])) {
        update_user_meta( $user_id, 'user_street', $_POST['user_street'] );
    }
    else {
        add_action('admin_notices', 'my_admin_notice');
        return false;
    }

}

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
不幸的是,什么也没有发生

我也尝试了添加设置错误,但我有同样的问题


有人能帮我一把吗?或者向我解释一下我做错了什么?非常感谢!多谢各位

添加管理员通知似乎为时已晚。另一种方法是使用在页面加载开始时运行的操作钩子load-$page,并检查用户meta是否为空。如果是,触发通知

add_action( 'load-profile.php', 'notice_so_23373245' );
add_action( 'load-user-edit.php', 'notice_so_23373245' );

function notice_so_23373245() 
{
    // Check to see if page is user-edit or profile
    $user = isset( $_GET['user_id'] ) ? $_GET['user_id'] : get_current_user_id();
    if( empty( get_user_meta( $user, 'user_street' ) ) )
        add_action('admin_notices', 'my_admin_notice');
}