問題描述

我有一個帖子類型,使用 post_save 從 post-meta 獲取地址,並從 Google API 檢索 lat /lng 座標。我需要一種方法來通知用户,如果檢索到協調對象有問題。我試過使用 admin_notices,但沒有顯示:

public static function update_notice() {
  echo "<div class='error'><p>Failed to retrieve coordinates. Please check key and address.<p></div>";
  remove_action('admin_notices', 'update_notice');
}

add_action('admin_notices', array('GeoPost', 'update_notice'));

我不知道我是否使用不正確或錯誤的上下文。要清楚,在實際代碼中,add_action 在同一個類中是另一個功能。這工作正常

最佳解決方案

這不行的原因是因為在 save_post 操作之後發生重定向。你想要的一種想法就是通過使用查詢變量實現一個快速的工作。

這是一個示範類示範:

class My_Awesome_Plugin {
  public function __construct(){
   add_action( 'save_post', array( $this, 'save_post' ) );
   add_action( 'admin_notices', array( $this, 'admin_notices' ) );
  }

  public function save_post( $post_id, $post, $update ) {
   // Do you stuff here
   // ...

   // Add your query var if the coordinates are not retreive correctly.
   add_filter( 'redirect_post_location', array( $this, 'add_notice_query_var' ), 99 );
  }

  public function add_notice_query_var( $location ) {
   remove_filter( 'redirect_post_location', array( $this, 'add_notice_query_var' ), 99 );
   return add_query_arg( array( 'YOUR_QUERY_VAR' => 'ID' ), $location );
  }

  public function admin_notices() {
   if ( ! isset( $_GET['YOUR_QUERY_VAR'] ) ) {
     return;
   }
   ?>
   <div class="updated">
      <p><?php esc_html_e( 'YOUR MESSAGE', 'text-domain' ); ?></p>
   </div>
   <?php
  }
}

希望這有助於您。乾杯

次佳解決方案

為這種場景做了一個包裝類。實際上,該類可以用於涉及顯示通知的任何情況。我使用 PSR 標準,所以命名是非典型的 Wordpress 代碼。

class AdminNotice
{
private static $instance;
const NOTICE_FIELD = 'derp_admin_notice_message';

protected function __construct() {}
private function __clone() {}
private function __wakeup() {}

static function getInstance()
{
    if (null === static::$instance) {
        static::$instance = new static();
    }

    return static::$instance;
}

public function displayAdminNotice()
{
    $option      = get_option(self::NOTICE_FIELD);
    $message     = isset($option['message']) ? $option['message'] : false;
    $noticeLevel = ! empty($option['notice-level']) ? $option['notice-level'] : 'notice-error';

    if ($message) {
        echo "<div class='notice {$noticeLevel} is-dismissible'><p>{$message}</p></div>";
        delete_option(self::NOTICE_FIELD);
    }
}

public function displayError($message)
{
    $this->updateOption($message, 'notice-error');
}

public function displayWarning($message)
{
    $this->updateOption($message, 'notice-warning');
}

public function displayInfo($message)
{
    $this->updateOption($message, 'notice-info');
}

public function displaySuccess($message)
{
    $this->updateOption($message, 'notice-success');
}

protected function updateOption($message, $noticeLevel) {
    update_option(self::NOTICE_FIELD, [
        'message' => $message,
        'notice-level' => $noticeLevel
    ]);
}
}

用法:

add_action('admin_notices', [AdminNotice::getInstance(), 'displayAdminNotice']);
$notice = AdminNotice::getInstance();
$notice->displayError(__('Better flee, an error occurred.', 'herp'));

通知顯示一次。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。