是否有任何方法可以禁用WP核心更新,但需要发送通知电子邮件,告知可以安装WP更新?我知道关于WP更新的通知电子邮件是在WP更新以及如何禁用所有(核心更新和通知)之后发送的。
我正在尝试使用以下代码:
function core_update_notification(){
global $wp_version;
$core_update = wp_version_check();
if ($core_update != NULL || $core_update !=FALSE){
$to = "[email protected]";
$subject = "WP update is available!";
$message = "You have WP update ready to install";
$headers = \'From: My Website <[email protected]>\' . "\\r\\n";
wp_mail( $to, $subject, $message, $headers );
}
}
add_action( \'init\', \'core_update_notification\' );
add_filter( \'auto_update_core\', \'__return_false\' );
但不知何故,这个想法并没有按我预期的方式发挥作用。
UPDATE #1看起来wp\\u version\\u check()执行版本检查,如果有新版本,则设置update\\u core transient。它不会返回任何有价值的东西,这就是上面的代码失败的原因。需要使用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 = "[email protected]";
$subject = "WP update is available!";
$message = "You have WP update ready to install";
$headers = \'From: My Website <[email protected]>\' . "\\r\\n";
wp_mail( $to, $subject, $message, $headers );
}
}
add_action( \'init\', \'core_update_notification\' );
add_filter( \'auto_update_core\', \'__return_false\' );
但不知何故,电子邮件没有发送
最合适的回答,由SO网友:JackTheKnife 整理而成
在切换代码以检查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 = "[email protected]";
$subject = "WP update is available!";
$message = "You have WP update ready to install";
$headers = \'From: My Website <[email protected]>\' . "\\r\\n";
wp_mail( $to, $subject, $message, $headers );
}
}
add_action( \'init\', \'core_update_notification\' );
add_filter( \'auto_update_core\', \'__return_false\' );
我认为最好的解决办法是每天使用
wp_schedule_event
在这种情况下,请执行以下步骤:
https://codex.wordpress.org/Function_Reference/wp_schedule_event