我创建了一个名为routes的自定义帖子类型,我希望能够在保存/更新过程中出现错误时将错误信息返回到屏幕,例如该类型允许上传gpx / kml文件并检查是否已发布正确的类型。目前它只是在它出错时返回 - 我该如何设置错误信息?
//Return if file type wrong.
if($file_type != 'application/octet-stream' && $file_type != 'application/gpx+xml' ) {
return;
}
答案 0 :(得分:3)
试试这个
示例:
add_admin_message('Please enter valid URL for the project link', true);
add_admin_message('Your custom post type was updated');
源:
<?php
/**
* Messages with the default wordpress classes
*/
function showMessage($message, $errormsg = false)
{
if ($errormsg) {
echo '<div id="message" class="error">';
}
else {
echo '<div id="message" class="updated fade">';
}
echo "<p>$message</p></div>";
}
/**
* Display custom messages
*/
function show_admin_messages()
{
if(isset($_COOKIE['wp-admin-messages-normal'])) {
$messages = strtok($_COOKIE['wp-admin-messages-normal'], "@@");
while ($messages !== false) {
showMessage($messages, true);
$messages = strtok("@@");
}
setcookie('wp-admin-messages-normal', null);
}
if(isset($_COOKIE['wp-admin-messages-error'])) {
$messages = strtok($_COOKIE['wp-admin-messages-error'], "@@");
while ($messages !== false) {
showMessage($messages, true);
$messages = strtok("@@");
}
setcookie('wp-admin-messages-error', null);
}
}
/**
* Hook into admin notices
*/
add_action('admin_notices', 'show_admin_messages');
/**
* User Wrapper
*/
function add_admin_message($message, $error = false)
{
if(empty($message)) return false;
if($error) {
setcookie('wp-admin-messages-error', $_COOKIE['wp-admin-messages-error'] . '@@' . $message, time()+60);
} else {
setcookie('wp-admin-messages-normal', $_COOKIE['wp-admin-messages-normal'] . '@@' . $message, time()+60);
}
}