问题描述

我有一个帖子类型,使用 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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。