2011-07-23 56 views
2

我創建了一個自定義帖子類型稱爲路由,我希望能夠在保存/更新過程中出現問題時返回錯誤消息到屏幕上。該類型允許上傳gpx/kml文件並檢查是否發佈了正確的類型。目前它只是返回,如果它出錯了 - 我怎麼能設置一個錯誤信息?WordPress的 - 爲自定義帖子類型創建自己的管理消息

//Return if file type wrong. 
if($file_type != 'application/octet-stream' && $file_type != 'application/gpx+xml') { 
return; 
} 

回答

2

試試這個

例如:

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); 
    } 
} 
相關問題