2012-03-21 2 views

답변

25

에서, 'admin_notices'후크 직접 사용할 수 없습니다, 왜냐하면 리디렉션이 있기 때문입니다. 해결 방법은 옵션 테이블에 통지를 저장하고 다음에이를 확인하는 것입니다. 또한 플러그인 업그레이드와 정품 인증을 함께 처리하려면 'admin_init'(WP 3.1부터 http://make.wordpress.org/core/2010/10/27/plugin-activation-hooks/ 참조)과 같은 다른 후크를 사용해야합니다.

다음은 정품 인증과 업그레이드를 모두 처리하는 샘플 플러그인입니다. 지연 통지를 배열로 만들었습니다. 그래서 배열을 쌓을 수 있습니다.

<?php 
/* 
Plugin Name: My Plugin 
*/ 

register_activation_hook(__FILE__, 'my_plugin_activation'); 
function my_plugin_activation() { 
    $notices= get_option('my_plugin_deferred_admin_notices', array()); 
    $notices[]= "My Plugin: Custom Activation Message"; 
    update_option('my_plugin_deferred_admin_notices', $notices); 
} 

add_action('admin_init', 'my_plugin_admin_init'); 
function my_plugin_admin_init() { 
    $current_version = 1; 
    $version= get_option('my_plugin_version'); 
    if ($version != $current_version) { 
    // Do whatever upgrades needed here. 
    update_option('my_plugin_version', $current_version); 
    $notices= get_option('my_plugin_deferred_admin_notices', array()); 
    $notices[]= "My Plugin: Upgraded version $version to $current_version."; 
    update_option('my_plugin_deferred_admin_notices', $notices); 
    } 
} 

add_action('admin_notices', 'my_plugin_admin_notices'); 
function my_plugin_admin_notices() { 
    if ($notices= get_option('my_plugin_deferred_admin_notices')) { 
    foreach ($notices as $notice) { 
     echo "<div class='updated'><p>$notice</p></div>"; 
    } 
    delete_option('my_plugin_deferred_admin_notices'); 
    } 
} 

register_deactivation_hook(__FILE__, 'my_plugin_deactivation'); 
function my_plugin_deactivation() { 
    delete_option('my_plugin_version'); 
    delete_option('my_plugin_deferred_admin_notices'); 
} 

UPDATE : 대신 update_option()set_transient()을 사용하고 올바른 관리 사용자에게 메시지를 직접하는 일반적인 방법도있다. 이 게시물이 문제의 메타 박스가 활성화 플러그인 아니지만, 기술은 내가 아는까지로, 거의 모든 곳에서 대시 보드에서 같은 일을 : https://wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from-a-meta-box-to-admin-notices

+0

이것은 좋은 것처럼 보입니다. 나의 유일한 의견은 새 공지를 추가하는 부분을 말리는 것입니다. – pguardiario

+0

예, 세 줄'$ notices = get_option (...); $ notices [] = ...; update_option (..., $ notices)'는 일반적인 목적의'my_plugin_add_notice()'함수로 추상화 될 수 있습니다. 'note'대 'error'매개 변수를 사용하여이 값을 볼 수 있습니다.그런 다음 올바르게 렌더링 한 경우 CSS 클래스 '업데이트'또는 '오류'를 사용하여 WP 또는 파란색 배너로 렌더링 함수를 표시합니다. – kitchin

3

그냥 <div class='updated'>을 사용하십시오. 예를 들어 -

echo "<div class='updated'>Test Plugin Notice</div>"; 
+0

네,하지만이 표시가 모든 시간을 보여주고, – Thompson

+1

이 경우 : 예를 들어

사용자가 플러그인 구성을 방문했는지 저장하는 플래그를 추가하기 만하면됩니다. 이 플래그를''wp_options'' 테이블에 저장할 수 있습니다. – ronakg

1

당신의주의 사항을 추가하는 적절한 방법 admin_notices 조치를 훅에 에코하는 것입니다 : 플러그인 활성화에 대한

function wpse8170_admin_notice(){ 
    echo '<div class="updated"><p>This is my notice.</p></div>'; 
} 
add_action('admin_notices', 'wpse8170_admin_notice'); 
+0

하지만 플러그인 활성화가 질문으로 표시되지 않습니다. – Progrock

7

이 통지를

function your_admin_notice(){ 
echo '<div class="updated"> 
    <p>I am a little yellow notice.</p>  
</div>';  
} 
add_action('admin_notices', 'your_admin_notice'); 

하지만 경우를 보여 매우 간단하다 면제 통보를 표시하고 다음을 시도하십시오

add_action('admin_notices', 'example_admin_notice'); 

function example_admin_notice() { 
    global $current_user ; 
     $user_id = $current_user->ID; 
     /* Check that the user hasn't already clicked to ignore the message */ 
    if (! get_user_meta($user_id, 'example_ignore_notice')) { 
     echo '<div class="updated"><p>'; 
     printf(__('This is an annoying nag message. Why do people make these? | <a href="%1$s">Hide Notice</a>'), '?example_nag_ignore=0'); 
     echo "</p></div>"; 
    } 
} 

add_action('admin_init', 'example_nag_ignore'); 

function example_nag_ignore() { 
    global $current_user; 
     $user_id = $current_user->ID; 
     /* If user clicks to ignore the notice, add that to their user meta */ 
     if (isset($_GET['example_nag_ignore']) && '0' == $_GET['example_nag_ignore']) { 
      add_user_meta($user_id, 'example_ignore_notice', 'true', true); 
    } 
} 

그리고 특정 페이지에 그 통지를 표시하려면 아래 조건을 시도하십시오.

function my_admin_notice(){ 
    global $pagenow; 
    if ($pagenow == 'plugins.php') { 
     echo '<div class="updated"> 
      <p>This notice only appears on the plugins page.</p> 
     </div>'; 
    } 
} 
add_action('admin_notices', 'my_admin_notice'); 

You can see here

+0

표시하려는 모든 메시지에 대해 콜백 함수를 만들어야한다는 사실을 올바르게 알고 있습니까? 오류 메시지의 내용을 지정하는 매개 변수를 사용하는 함수는 어떻게 만들 수 있습니까? – majikman

+0

오류 메시지를 표시하려면 실제로 다른 방법이 있습니다. 매개 변수가있는 admin_notice를 표시하려면 여기에서 가장 많이 대답 할 수 있습니다. 또한 아래 링크에서 방법을 찾을 수 있습니다 http://stackoverflow.com/questions/1242328/wordpress-displaying-an-error-message-hook-admin-notices-fails-on-wp-insert-p –

0

것은 내가 amarkal-admin-notification 개발했습니다 - 정적/취소 가능한 관리자의주의 사항을 추가 할 수 있습니다 당신을 위해 해고를 처리하는 스크립트. 이 스크립트는 Amarkal WordPress 프레임 워크 내의 모듈입니다. 내가 (활성화 한 후) 그 통지의 설정 링크를 클릭이 통지가 사라질 것처럼 내가 원하는

amarkal_admin_notification('my-error-notice', __('Oh snap! This is an <strong>error</strong> message.','slug'), 'error'); 
관련 문제