問題描述

是否有任何方法來停用 WP 核心更新,但傳送通知郵件 WP 更新可用於安裝?我知道關於 WP 更新的通知電子郵件是在 WP 更新後的事實以及如何停用所有 (核心更新和通知) 之後傳送的。

我正在嘗試使用以下程式碼:

function core_update_notification(){
    global $wp_version;

   $core_update = wp_version_check();

   if ($core_update != NULL || $core_update !=FALSE){
      $to = "me@example.com";
      $subject = "WP update is available!";
      $message = "You have WP update ready to install";
      $headers = 'From: My Website <myname@mywebsite.com>' . "rn";

      wp_mail( $to, $subject, $message, $headers );
   }

}

   add_action( 'init', 'core_update_notification' );
   add_filter( 'auto_update_core', '__return_false' );

但不知何故,這個想法不是我所期望的方式。

更新#1 看起來像是這樣的, wp_version_check() 執行版本檢查,然後設定 update_core 瞬態,如果有新版本。它不返回任何有價值的東西,這就是為什麼上面的程式碼失敗。需要使用 get_site_transient('update_core')和更新的程式碼看起來像

function core_update_notification(){
   global $wp_version;

   $installed_version = $wp_version;

   $uc_transient = get_site_transient('update_core');
   if ( empty($uc_transient->updates) ) return;
   $updates = $uc_transient->updates;
   $current_version = $updates[0]->current;

   if (version_compare($installed_version, $current_version, "<")) {
      $to = "me@example.com";
      $subject = "WP update is available!";
      $message = "You have WP update ready to install";
      $headers = 'From: My Website <myname@mywebsite.com>' . "rn";

      wp_mail( $to, $subject, $message, $headers );
   }

}

   add_action( 'init', 'core_update_notification' );
   add_filter( 'auto_update_core', '__return_false' );

但不知何故不傳送電子郵件

最佳解決辦法

看起來像切換程式碼來檢查 get_site_transient('update_core')我已經得到了我需要的只是動作必須在適當的優先順序或鉤子 (想法/建議?) 執行。下面的工作程式碼:

function core_update_notification(){
   global $wp_version;

   $installed_version = $wp_version;

   $uc_transient = get_site_transient('update_core');
   if ( empty($uc_transient->updates) ) return;
   $updates = $uc_transient->updates;
   $current_version = $updates[0]->current;

   if (version_compare($installed_version, $current_version, "<")) {
      $to = "me@example.com";
      $subject = "WP update is available!";
      $message = "You have WP update ready to install";
      $headers = 'From: My Website <myname@mywebsite.com>' . "rn";

      wp_mail( $to, $subject, $message, $headers );
   }

}

   add_action( 'init', 'core_update_notification' );
   add_filter( 'auto_update_core', '__return_false' );

我認為最好的解決方案是使用 wp_schedule_event 每日 wp_schedule_event,從下列步驟:https://codex.wordpress.org/Function_Reference/wp_schedule_event

參考文獻

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