WordPress函数:get_current_user_id 获取当前登录用户的ID

江河/ 2023年08月27日/ WordPress/ 浏览 574

函数原型:


get_current_user_id(): int


获取当前登录用户的ID。


函数源码:


function get_current_user_id() {
	if ( ! function_exists( 'wp_get_current_user' ) ) {
		return 0;
	}
	$user = wp_get_current_user();
	return ( isset( $user->ID ) ? (int) $user->ID : 0 );
}


使用举例:


if ( is_user_logged_in() ) {
    echo 'User ID: ' . get_current_user_id();
} else {
    echo 'Hello visitor!';
}


发表评论

暂无评论,抢个沙发...

客服 工单