2012-03-21 37 views

Trả lời

25

Đối với kích hoạt plugin, móc 'admin_notices' không thể được sử dụng trực tiếp, bởi vì có một chuyển hướng. Giải pháp thay thế là lưu trữ thông báo của bạn trong bảng tùy chọn và kiểm tra thông báo trong lần tiếp theo. Ngoài ra, nếu bạn cũng muốn bao gồm nâng cấp plugin cũng như kích hoạt, bạn sẽ cần phải sử dụng một móc khác, chẳng hạn như 'admin_init' (kể từ WP 3.1, xem http://make.wordpress.org/core/2010/10/27/plugin-activation-hooks/).

Dưới đây là một plugin mẫu hoàn chỉnh xử lý cả kích hoạt và nâng cấp. Tôi đã thực hiện thông báo trì hoãn một mảng để bạn có thể xếp chúng lên.

<?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: Ngoài ra còn có một cách phổ biến để sử dụng set_transient() thay vì update_option(), và chỉ đạo thông điệp cho người dùng quản trị chính xác. Đây bài lo ngại metaboxes, không cắm kích hoạt, nhưng các kỹ thuật làm việc như nhau chỉ là về ở khắp mọi nơi trong Bảng điều khiển, như xa như tôi biết: https://wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from-a-meta-box-to-admin-notices

+0

Điều này có vẻ tốt, bình luận duy nhất của tôi sẽ là DRY lên phần mà bạn thêm một thông báo mới. – pguardiario

+0

Đúng, ba dòng '$ thông báo = get_option (...); $ thông báo [] = ...; update_option (..., $ notices) 'có thể được abstracted ra cho mục đích chung' my_plugin_add_notice() 'chức năng. Bạn thường thấy điều này với một tham số cho 'note' so với 'error'.Sau đó, chức năng hiển thị sau đó hiển thị nó trong thời trang WP dưới dạng biểu ngữ màu xanh dương hoặc màu đỏ, sử dụng lớp css 'cập nhật' hoặc 'lỗi', nếu tôi nhớ chính xác. – kitchin

3

Chỉ cần sử dụng <div class='updated'>. Ví dụ:

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

có, nhưng sau đó thông báo này được hiển thị tất cả các lần, tôi muốn như thông báo này sẽ biến mất, khi tôi bấm vào liên kết config trong thông báo đó (sau khi kích hoạt) – Thompson

+1

Trong trường hợp đó bạn chỉ cần thêm một lá cờ sẽ lưu trữ nếu người dùng truy cập vào cấu hình của plugin. Bạn có thể lưu trữ cờ này trong bảng '' wp_options''. – ronakg

1

Cách thích hợp để thêm thông báo của bạn là để echo nó trong móc của bạn cho admin_notices hành động:

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

Nhưng nó không hiển thị trên kích hoạt plugin làm câu hỏi. – Progrock

7

Đây là đơn giản như vậy để hiển thị một thông báo

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

Nhưng nếu bạn muốn hiển thị Thông báo không được chấp nhận, hãy thử bên dưới

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

Và nếu bạn muốn hiển thị thông báo đó trên trang nhất định, hãy thử dưới điều kiện.

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

Tôi có thấy chính xác điều này mà tôi cần tạo chức năng gọi lại cho mọi thư tôi muốn hiển thị không? Làm thế nào tôi có thể tạo ra một hàm có tham số chỉ định thông báo lỗi là gì? – majikman

+0

Ok, nếu bạn muốn hiển thị thông báo lỗi thì có một cách khác thực sự. Để hiển thị admin_notice với tham số, bạn có thể thử câu trả lời hay nhất ở đây. Ngoài ra, bạn có thể tìm cách từ các liên kết bên dưới http://stackoverflow.com/questions/1242328/wordpress-displaying-an-error-message-hook-admin-notices-fails-on-wp-insert-p –

0

tôi đã phát triển amarkal-admin-notification - một kịch bản cho phép bạn thêm/thông báo quản trị loại bỏ được tĩnh và xử lý các sa thải cho bạn. Kịch bản này là một mô-đun trong khuôn khổ WordPress Amarkal.

Ví dụ:

amarkal_admin_notification('my-error-notice', __('Oh snap! This is an <strong>error</strong> message.','slug'), 'error'); 
Các vấn đề liên quan