2012-03-21 19 views
13

जब मैं अपना परीक्षण प्लगइन सक्रिय करता हूं, तो मैं व्यवस्थापक पैनल में एक नोटिस प्रदर्शित करने का प्रयास कर रहा हूं। मैं इसे कैसे प्रदर्शित कर सकता हूं? क्या कोई उस विधि को जानता है?वर्डप्रेस: ​​प्लगइन सक्रियण पर, व्यवस्थापक पैनल में नोटिस कैसे प्रदर्शित करें?

उत्तर

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_option() के बजाय set_transient() उपयोग करने के लिए, और सही व्यवस्थापक उपयोगकर्ता को संदेश निर्देशित करने के लिए है। इस पोस्ट में चिंताओं metaboxes, सक्रियण प्लगइन नहीं है, लेकिन तकनीक ही काम के बारे में डैशबोर्ड में हर जगह, मैं जानता हूँ कि जहाँ तक: https://wordpress.stackexchange.com/questions/15354/passing-error-warning-messages-from-a-meta-box-to-admin-notices

+0

यह अच्छा लग रहा है, मेरी एकमात्र टिप्पणी उस हिस्से को ड्रॉ करना होगा जहां आप एक नई सूचना जोड़ते हैं। – pguardiario

+0

हां, तीन पंक्तियां '$ नोटिस = get_option (...); $ नोटिस [] = ...; update_option (..., $ नोटिस) 'सामान्य उद्देश्य' my_plugin_add_notice() 'फ़ंक्शन पर समझा जा सकता है। आप इसे अक्सर 'नोट' बनाम 'त्रुटि' के पैरामीटर के साथ देखते हैं।फिर फिर रेंडरिंग फ़ंक्शन इसे WP शैली में एक नीले या लाल बैनर के रूप में प्रदर्शित करता है, सीएसएस क्लास 'अपडेट' या 'त्रुटि' का उपयोग करके, अगर मुझे सही याद आती है। – kitchin

3

बस <div class='updated'> का उपयोग करें। उदाहरण के लिए -

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

हाँ, लेकिन फिर इस नोटिस हर समय दिखा रहा है, मैं चाहता हूँ इस नोटिस की तरह गायब हो जाएगा, जब मैं उस नोटिस में config लिंक पर क्लिक करें (सक्रिय करने के बाद) उस मामले में – 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 विकसित किया है - एक स्क्रिप्ट आप स्थिर/dismissible व्यवस्थापक नोटिस जोड़ सकते हैं और आप के लिए बर्खास्तगी संभालती है कि। यह स्क्रिप्ट अमरक वर्डप्रेस ढांचे के भीतर एक मॉड्यूल है।

उदाहरण के लिए:

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