현재 사용자가 wordpress에서 관리자인지 확인합니다.
워드프레스용 플러그인을 개발 중인데 현재 사용자가 관리자인지 확인하고 싶은데 아쉽게도 사용할 수 없었습니다.current_user_can()에러가 나기 때문에 글로벌을 사용하고 있습니다.$current_user하지만 admin 사용자도 if 파트에 들어갈 수 없었습니다.어떻게 고칠까요?
global $current_user;
if ($current_user->role[0]=='administrator'){
function hide_post_page_options() {
//global $post;
// Set the display css property to none for add category and add tag functions
$hide_post_options = "<style type=\"text/css\"> .jaxtag { display: none; } #category-adder { display: none; } </style>";
print($hide_post_options);
}
add_action( 'admin_head', 'hide_post_page_options' );
}
다음과 같은 방법을 시도해 보십시오.
if ( current_user_can( 'manage_options' ) ) {
/* A user with admin privileges */
} else {
/* A user without admin privileges */
}
자세한 내용은current_user_can여기서 기능합니다.
사용자를 가져와 다음과 같이 관리자 역할이 있는지 확인합니다.
function is_site_admin(){
return in_array('administrator', wp_get_current_user()->roles);
}
if (is_site_admin()) {
echo 'Woot Woot';
} else {
echo 'So sad, I have no rights';
}
이것으로 충분합니다.
global $current_user;
if( !empty($current_user->roles) ){
foreach ($current_user->roles as $key => $value) {
if( $value == 'administrator' ){
Do Something
}
}
}
다중 사이트 설정이 아닌 경우 이 설정을 사용하여 관리자를 검색할 수 있습니다.멀티 사이트인 경우 슈퍼 관리자만 true로 반환됩니다.
$user_ID = get_current_user_id();
if($user_ID && is_super_admin( $user_id )) {
Do Something
}
오래된 질문인 것은 알지만, 이 페이지를 실제 문제에 대처함으로써 더 유용하게 만들고 싶습니다.여기서의 실제적인 문제는 OP가 이 기능을 사용할 수 없다는 것입니다.current_user_can( 'manage_options' )그의 플러그인에서.이 기능을 사용하면 평상시보다 높아집니다.undefined function...PHP 오류입니다.이는 WP core 로드가 완료되기 전에 플러그인이 초기화되기 때문입니다.수정은 매우 간단합니다.적절한 시기에 플러그인을 로드하는 것이 중요합니다.
admin 플러그인 코드가 클래스 내에 있다고 가정합니다.MyPlugin, 클래스 초기화는 에 접속할 필요가 있습니다.init다음은 한 가지 방법입니다.
/**
* Plugin Class
*/
class MyPlugin{
public function __construct(){
/* all hooks and initialization stuff here */
/* only hook if user has appropriate permissions */
if(current_user_can('manage_options')){
add_action( 'admin_head', array($this, 'hide_post_page_options'));
}
}
function hide_post_page_options() {
// Set the display css property to none for add category and add tag functions
$hide_post_options = "
<style type=\"text/css\">
.jaxtag { display: none; }
#category-adder { display: none; }
</style>";
print($hide_post_options);
}
}
add_action('admin_init', function(){
$myplugin = new MyPlugin();
});
이는 플러그인 함수가 워드프레스 코어를 사용할 수 있도록 하는 방법입니다.
찾을 수 있습니다.admin_init매뉴얼을 참조하십시오.
추신: PHP GREEDOC 사용을 검토해야 합니다.이것은 여러 줄의 문자열을 쓰는 매우 간단한 방법입니다.스타일 블록을 다음과 같이 다시 작성할 수 있습니다.
$hide_post_options = <<<HTML
<style type="text/css">
.jaxtag { display: none; }
#category-adder { display: none; }
</style>
HTML;
도움이 됐으면 좋겠어요.
감사해요.
이 질문에 대한 답을 하기엔 너무 늦었지만, 누군가 나처럼 여기 오게 된다면 어쨌든 도움이 될 것 같아.
이 문제에 대한 빠른 해결책이 필요했습니다. 현재 사용자가 관리자인지 확인하십시오.
WP codex에서 사용할 간단한 해결책을 얻었습니다.
if(is_super_admin($user_id)) {
// do stuff for the admin user...
}
WP-Codex에 따르면 이 함수는 현재 로그인한 사용자가 네트워크(슈퍼) 관리자일 경우 True를 반환합니다.이 함수는 네트워크 모드가 비활성화되어 있지만 현재 사용자가 admin인 경우에도 True를 반환합니다.
<?php
if( current_user_can( 'administrator' ) ){} // only if administrator
if( current_user_can( 'editor' ) ){} // only if editor
if( current_user_can( 'author' ) ){} // only if author
if( current_user_can( 'contributor' ) ){} // only if contributor
if( current_user_can( 'subscriber' ) ){} // only if subscriber
?>
자세한 내용은 이쪽:WordPress에서 사용자가 관리자인지 에디터인지 확인하는 방법
이 코드를 사용하세요. 이것이 당신의 문제를 해결하길 바랍니다.
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
echo trim($user_role);
$user=wp_get_current_user();
if(in_array("administrator", $user->roles)){
//user role is admin
}
언급URL : https://stackoverflow.com/questions/19802492/check-if-current-user-is-administrator-in-wordpress
'source' 카테고리의 다른 글
| 재생 프레임워크 JsObject에서 Json 배열 구문 분석 (0) | 2023.04.02 |
|---|---|
| 제품 ID에 대한 PHP 변수와 함께 wc_get_product() 사용 (0) | 2023.04.02 |
| Wordpress에서 XSLT를 사용한 XML 구문 분석 (0) | 2023.04.02 |
| WordPress post 추가/업데이트 후 훅과 포스트 메타 삽입 후 후 후크? (0) | 2023.04.02 |
| Excel이 특정 텍스트 값을 날짜로 자동 변환하지 않도록 합니다. (0) | 2023.04.02 |