WordPress에 출력 HTML 및 형식을 표시하고 일부 입력의 유효성을 검사하고 DB에 저장하는 새 관리자 페이지를 등록했습니다. 모든 작품은 훌륭합니다. 실제로 작동하지 않는 것은 admin_notices 훅을 사용하여 오류/업데이트 메시지를 표시하는 것입니다. 나는 add_menu_page 함수 내에서 그 훅을 호출 할 수 없다고 추측한다. 그렇다면 어떻게 OOP 플러그인의 맞춤 페이지에서 오류를 처리한다고 생각하십니까?OOP 플러그인에서 WordPress admin_notices 사용
아래 코드를 제거했습니다.
class Fitems_Admin {
public $admin_notices = array();
public function __construct(){
// Register Menus
add_action('admin_menu', array($this, 'register_menus'));
}
public function register_menus(){
add_menu_page('fItems', 'fItems', 'administrator', 'fItems', array($this, 'items'));
}
public function items(){
// do stuff, validate input & register any errors, example below
$this->admin_notices['updated'][] = __('item(s) successfully deleted.', 'fItems');
// Register errors
add_action('admin_notices', array($this , 'display_admin_notices'));
// display output;
echo $output;
}
// Just formats and echos any errors in the $this->admin_notices array();
public function display_admin_notices($return = FALSE){
if(! empty($this->admin_notices)){
// Remove an empty and then sort
array_filter($this->admin_notices);
ksort($this->admin_notices);
$output = '';
foreach($this->admin_notices as $key => $value){
// Probably an array but best to check
if(is_array($value)){
foreach($value as $v){
$output .= '<div class="' . esc_attr($key) . '"><p>' . esc_html($v) . '</p></div>';
}
} else {
$output .= '<div class="' . esc_attr($key) . '"><p>' . esc_html($value) . '</p></div>';
}
}
if($return){
return $output;
} else {
echo $output;
}
}
}
}
시도해 볼 수도 있습니다. 모든주의 사항을'$ error_notices' 변수에 저장합니다. $ error_notices = $ this-> admin_notices [ 'updated'] [] = __ ('항목이 성공적으로 삭제되었습니다.', 'fItems'); '기능 my_admin_notice() {' '전역의 $ error_notices;' 가 '$ error_notices 에코;' '} ' 'ADD_ACTION ('admin_notices '$ this-> my_admin_notice)' –