問題描述
我有一個簡單的元框來更新帖子自定義欄位 (使用 update_post_meta()) 。
在使用者釋出/更新帖子之後,如何傳送錯誤或警告訊息,並且不填寫其中一個元框欄位 (或填寫無效資料)?
最佳解決方案
你可以手動執行,但 WP 本身就是這樣設定錯誤:
-
add_settings_error()建立訊息。 -
然後
set_transient('settings_errors', get_settings_errors(), 30); -
settings_errors()在admin_notices掛鉤顯示 (需要掛鉤 non-settings 螢幕) 。
次佳解決方案
您可以使用 admin_notices 鉤
首先定義通知功能:
function my_admin_notice(){
//print the message
echo '<div id="message">
<p>metabox as errors on save message here!!!</p>
</div>';
//make sure to remove notice after its displayed so its only displayed when needed.
remove_action('admin_notices', 'my_admin_notice');
}
你可以根據需要新增 metabox 儲存功能:
...
...
if($errors){
add_action('admin_notices', 'my_admin_notice');
}
...
...
Update
像我在這裡答應的一個例子,我如何新增我的 metabox 的錯誤訊息
<?php
/*
Plugin Name: one-trick-pony-notice
Plugin URI: http://en.bainternet.info
Description: Just to proof a point using admin notice form metabox
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/
/* admin notice */
function my_admin_notice(){
//print the message
global $post;
$notice = get_option('otp_notice');
if (empty($notice)) return '';
foreach($notice as $pid => $m){
if ($post->ID == $pid ){
echo '<div id="message" class="error"><p>'.$m.'</p></div>';
//make sure to remove notice after its displayed so its only displayed when needed.
unset($notice[$pid]);
update_option('otp_notice',$notice);
break;
}
}
}
//hooks
add_action('add_meta_boxes', 'OT_mt_add');
add_action('save_post', 'OT_mt_save');
add_action('admin_notices', 'my_admin_notice',0);
//add metabox
function OT_mt_add() {
add_meta_box('OT_mt_sectionid', __( 'One Trick Meta Box notice', 'textdomain' ),'OT_mt_display','post');
}
//display metabox
function OT_mt_display() {
// Use nonce for verification
wp_nonce_field( plugin_basename(__FILE__), 'myplugin_noncename' );
// The actual fields for data entry
echo '<label for="myplugin_new_field">';
_e("leave blank to get a notice on publish or update", 'textdomain' );
echo '</label> ';
echo '<input type="text" id="ot_field" name="ot_field" value="" size="25" />';
}
//save metabox here is were i check the fields and if empty i display a message
function OT_mt_save( $post_id ) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if (!isset($_POST['myplugin_noncename'])) return $post_id;
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) ) )
return $post_id;
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
if(!isset($_POST['ot_field']) || empty($_POST['ot_field'])){
//field left empty so we add a notice
$notice = get_option('otp_notice');
$notice[$post_id] = "You have left the field empty";
update_option('otp_notice',$notice);
}
}
現在,當找到這個程式碼時,我發現我的舊方式使用 post_updated_messages 過濾器鉤子,大致相同的方式,所以我也新增:
<?php
/*
Plugin Name: one-trick-pony-notice2
Plugin URI: http://en.bainternet.info
Description: just like the one above but this time using post_updated_messages hook
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/
//hooks
add_filter('post_updated_messages','my_messages',0);
add_action('add_meta_boxes', 'OT_mt_add');
add_action('save_post', 'OT_mt_save');
//add metabox
function OT_mt_add() {
add_meta_box('OT_mt_sectionid', __( 'One Trick Meta Box notice', 'textdomain' ),'OT_mt_display','post');
}
//display metabox
function OT_mt_display() {
// Use nonce for verification
wp_nonce_field( plugin_basename(__FILE__), 'myplugin_noncename' );
// The actual fields for data entry
echo '<label for="myplugin_new_field">';
_e("leave blank to get a notice on publish or update", 'textdomain' );
echo '</label> ';
echo '<input type="text" id="ot_field" name="ot_field" value="" size="25" />';
}
//save metabox here is were i check the fields and if empty i display a message
function OT_mt_save( $post_id ) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if (!isset($_POST['myplugin_noncename'])) return $post_id;
if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) ) )
return $post_id;
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
if(!isset($_POST['ot_field']) || empty($_POST['ot_field'])){
//field left empty so we add a notice
$notice = get_option('otp_notice');
$notice[$post_id] = "You have left the field empty";
update_option('otp_notice',$notice);
}
}
//messages filter
function my_messages($m){
global $post;
$notice = get_option('otp_notice');
if (empty($notice)) return $m;
foreach($notice as $pid => $mm){
if ($post->ID == $pid ){
foreach ($m['post'] as $i => $message){
$m['post'][$i] = $message.'<p>'.$mm.'</p>';
}
unset($notice[$pid]);
update_option('otp_notice',$notice);
break;
}
}
return $m;
}
第三種解決方案
來自 WP Tavern 的 Otto 的 This answer [mirror] 實際上透過執行 WordPress 本身來克服重定向問題來解決瞬態問題。完全為我工作
The problem is that transients are there for everybody. If you have more than one user doing things at the same time, the error message can go to the wrong person. It’s a race condition.
WordPress actually does this by passing a message parameter in the URL. The message number indicates which message to display.
You can do the same by hooking the
redirect_post_locationfilter and then usingadd_query_argto add your own parameter to the request. Like so:add_filter('redirect_post_location','my_message'); function my_message($loc) { return add_query_arg( 'my_message', 123, $loc ); }This adds
my_message=123to the query. Then, after the redirect, you can detect the my_message setting in the$_GETand display the proper message accordingly.
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。