current_user_can

函式
current_user_can ( $capability, $args )
引數
  • (string) $capability Capability name.
    Required:
  • (mixed) $args Optional further parameters, typically starting with an object ID.
    Required:
返回值
  • (bool) Whether the current user has the given capability. If `$capability` is a meta cap and `$object_id` is passed, whether the current user has the given meta capability for the given object.
相關
  • WP_User::has_cap()
  • map_meta_cap()
定義位置
相關方法
current_user_can_for_blogset_current_userget_current_user_idwp_get_current_userwp_set_current_user
引入
2.0.0
棄用
-

current_user_can: 這個函式檢查當前使用者是否有特定的許可權,如編輯文章或管理選項。它通常用於WordPress程式碼中,根據使用者角色和許可權限制對某些功能或內容的訪問。

返回當前使用者是否擁有指定的許可權。

這個函式也接受一個物件的ID,以檢查該許可權是否是元許可權。元許可權,如`edit_post`和`edit_user`是由`map_meta_cap()`函式用來對映到一個使用者或角色擁有的原始許可權,如`edit_posts`和`edit_others_posts`。

使用例項:
current_user_can( ‘edit_posts’ );
current_user_can( ‘edit_post’, $post->ID );
current_user_can( ‘edit_post_meta’, $post->ID, $meta_key );

雖然部分支援用特定的角色代替許可權進行檢查,但不鼓勵這種做法,因為它可能產生不可靠的結果。

注意: 如果當前使用者是超級管理員,將總是返回true,除非特別拒絕。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function current_user_can( $capability, ...$args ) {
return user_can( wp_get_current_user(), $capability, ...$args );
}
function current_user_can( $capability, ...$args ) { return user_can( wp_get_current_user(), $capability, ...$args ); }
function current_user_can( $capability, ...$args ) {
	return user_can( wp_get_current_user(), $capability, ...$args );
}

常見問題

FAQs
檢視更多 >