2012-03-21 9 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()を使用するには、正しい管理者ユーザーにメッセージを指示する一般的な方法もあります。この投稿への懸念のmetaboxesは、アクティベーションプラグインではないが、技術は、私が知る限り、ちょうど約どこでもダッシュボードで同じ仕事:https://wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from-a-meta-box-to-admin-notices

+0

これはいいようです、私の唯一のコメントは、あなたが新しい通知を追加する部分を乾かすことです。 – pguardiario

+0

うん、3行 '$ notices = get_option(...); $ notices [] = ...; update_option(...、$ notices) 'を汎用目的の' my_plugin_add_notice() '関数に抽象化することができます。これは、 'note'と 'error'のパラメータでよく見られます。それから、そのレンダリング関数は、青色または赤色のバナーとしてWPファッションで、cssクラスの 'update'または 'error'を使用して正しく表示します。 – kitchin

3

<div class='updated'>を使用してください。たとえば -

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

はい、私は(活性化した後に)その通知に設定リンクをクリックしたとき、この通知は、この通知が消えますように私はしたい、すべての回を示している – Thompson

+1

その場合、ユーザーがプラグインの設定を訪問した場合に格納するフラグを追加するだけで済みます。このフラグを '' wp_options''テーブルに格納することができます。 – ronakg

1

適切な方法は、admin_noticesアクションのあなたのフックでそれをエコーすることです:

Linkyは、プラグインのアクティベーションのために

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); 
    } 
} 

の下に試してみて、あなたが条件以下の特定のページの試行でその通知を表示したい場合は、その後Dismissibleお知らせを表示したいです。

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をIを開発しました - あなたは、静的/ dismissible管理者通知を追加することができますし、あなたのために解雇を処理するスクリプトを。このスクリプトは、Amarkal WordPressフレームワーク内のモジュールです。例えば

amarkal_admin_notification('my-error-notice', __('Oh snap! This is an <strong>error</strong> message.','slug'), 'error'); 
関連する問題