2013-10-28 2 views
2

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; 
      } 
     } 
    } 
} 
+0

시도해 볼 수도 있습니다. 모든주의 사항을'$ error_notices' 변수에 저장합니다. $ error_notices = $ this-> admin_notices [ 'updated'] [] = __ ('항목이 성공적으로 삭제되었습니다.', 'fItems'); '기능 my_admin_notice() {' '전역의 $ error_notices;' 가 '$ error_notices 에코;' '} ' 'ADD_ACTION ('admin_notices '$ this-> my_admin_notice)' –

답변

3

관리자 용 알림은이 용도로 사용되지 않으며 퍼팅하는 시점에서 후크가 작동하지 않습니다. 선택적으로 실행하려면 get_option 또는 get_user_meta을 사용합니다.

if($success) 
    echo '<div class="updated"><p>class .updated with paragraph</p></div>'; 

if($error) 
    echo '<div class="error"><p>class .error with paragraph</p></div>'; 

여기에 우리의 관리자 화면을 스타일 도우미 플러그인입니다 : WordPress Admin Styles통지 이런 종류의

, 우리는 관리 클래스를 사용합니다.

하거나, 이름에도 불구하고, updateerror 메시지를 표시하는 데 사용됩니다,주의 사항을 표시하는 기능 add_settings_error()을 가지고 전체 Settings API를 사용합니다.

+0

HUMM 확인 그래서 나는 그것을 처음 정확하게하고 있었다. 가능한 한 많은 WordPress 기능을 사용하기 위해 약간의 통합을 시도 할 수 있습니다! 감사. – OzTheGreat

+1

@Augustud, Settings API에 대해 언급하는 것을 잊어 버렸습니다 (업데이트 참조). – brasofilo

+0

흥미 롭습니다. 실제로 설정 페이지가 아니며 (또는 설정 API를 사용합니다.) 사용자 정의 양식입니다. 질문은 add_settings_error()가 설정 API와 함께 사용해야하거나 일반적인 오류/알림 기능으로 사용해야하는지 여부입니다. 나는 이전을 생각하고 있나? – OzTheGreat

관련 문제