author_can

函式
author_can ( $post, $capability, $args )
引數
  • (int|WP_Post) $post Post ID or post object.
    Required:
  • (string) $capability Capability name.
    Required:
  • (mixed) $args Optional further parameters, typically starting with an object ID.
    Required:
返回值
  • (bool) Whether the post author has the given capability.
定義位置
相關方法
user_canthe_author_icqthe_author_msnthe_author_aimget_author_name
引入
2.9.0
棄用
-

author_can: 這個函式用於檢查某個使用者是否可以在一個文章、頁面或自定義文章型別上執行特定的操作。它需要兩個引數:第一個是被檢查的許可權,第二個是文章的ID。如果使用者有這種許可權,它將返回true。如果沒有,它將返回false。

返回所提供的文章的作者是否具有指定的許可權。

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

使用例項:

author_can( $post, ‘edit_posts’ );
author_can( $post, ‘edit_post’, $post->ID );
author_can( $post, ‘edit_post_meta’, $post->ID, $meta_key );

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function author_can( $post, $capability, ...$args ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
$author = get_userdata( $post->post_author );
if ( ! $author ) {
return false;
}
return $author->has_cap( $capability, ...$args );
}
function author_can( $post, $capability, ...$args ) { $post = get_post( $post ); if ( ! $post ) { return false; } $author = get_userdata( $post->post_author ); if ( ! $author ) { return false; } return $author->has_cap( $capability, ...$args ); }
function author_can( $post, $capability, ...$args ) {
	$post = get_post( $post );
	if ( ! $post ) {
		return false;
	}

	$author = get_userdata( $post->post_author );

	if ( ! $author ) {
		return false;
	}

	return $author->has_cap( $capability, ...$args );
}

常見問題

FAQs
檢視更多 >