wordpress未能与站点联系来检查致命错误,因此PHP修改已被回滚

阿里云代金券 腾讯云代金券 VULTR优惠码 Ucloud云主机

文章目录[隐藏]

如果你也遇到wordpress后台编辑主题文件时,出现“未能与站点联系来检查致命错误,因此PHP修改已被回滚。您需要采用其他方式(如SFTP)上传您修改的PHP文件”的提示,可以按照下面方法解决。

解决方案

1、找到wp-admin/includes/file.php文件:

首先备份一份原文件,然后打开这个文件,删除部分代码,即可解决wordpress后台无法编辑主题的错误。

2、找到并删除以下代码:

根据版本不同,位置并不固定,不过一般在490行以后,616行之前,大家可以用 ctrl+F 键找一下。

if ( $is_active && 'php' === $extension ) {

$scrape_key = md5( rand() );
$transient = 'scrape_key_' . $scrape_key;
$scrape_nonce = (string) rand();
// It shouldn't take more than 60 seconds to make the two loopback requests.
set_transient( $transient, $scrape_nonce, 60 );

$cookies = wp_unslash( $_COOKIE );
$scrape_params = array(
'wp_scrape_key' => $scrape_key,
'wp_scrape_nonce' => $scrape_nonce,
);
$headers = array(
'Cache-Control' => 'no-cache',
);

/** This filter is documented in wp-includes/class-wp-http-streams.php */
$sslverify = apply_filters( 'https_local_ssl_verify', false );

// Include Basic auth in loopback requests.
if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
}

// Make sure PHP process doesn't die before loopback requests complete.
set_time_limit( 300 );

// Time to wait for loopback requests to finish.
$timeout = 100;

$needle_start = "###### wp_scraping_result_start:$scrape_key ######";
$needle_end = "###### wp_scraping_result_end:$scrape_key ######";

// Attempt loopback request to editor to see if user just whitescreened themselves.
if ( $plugin ) {
$url = add_query_arg( compact( 'plugin', 'file' ), admin_url( 'plugin-editor.php' ) );
} elseif ( isset( $stylesheet ) ) {
$url = add_query_arg(
array(
'theme' => $stylesheet,
'file' => $file,
),
admin_url( 'theme-editor.php' )
);
} else {
$url = admin_url();
}

if ( function_exists( 'session_status' ) && PHP_SESSION_ACTIVE === session_status() ) {
// Close any active session to prevent HTTP requests from timing out
// when attempting to connect back to the site.
session_write_close();
}

$url = add_query_arg( $scrape_params, $url );
$r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
$body = wp_remote_retrieve_body( $r );
$scrape_result_position = strpos( $body, $needle_start );

$loopback_request_failure = array(
'code' => 'loopback_request_failed',
'message' => __( 'Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.' ),
);
$json_parse_failure = array(
'code' => 'json_parse_error',
);

$result = null;

if ( false === $scrape_result_position ) {
$result = $loopback_request_failure;
} else {
$error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
$error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
$result = json_decode( trim( $error_output ), true );
if ( empty( $result ) ) {
$result = $json_parse_failure;
}
}

// Try making request to homepage as well to see if visitors have been whitescreened.
if ( true === $result ) {
$url = home_url( '/' );
$url = add_query_arg( $scrape_params, $url );
$r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
$body = wp_remote_retrieve_body( $r );
$scrape_result_position = strpos( $body, $needle_start );

if ( false === $scrape_result_position ) {
$result = $loopback_request_failure;
} else {
$error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
$error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
$result = json_decode( trim( $error_output ), true );
if ( empty( $result ) ) {
$result = $json_parse_failure;
}
}
}

delete_transient( $transient );

if ( true !== $result ) {
// Roll-back file change.
file_put_contents( $real_file, $previous_content );
wp_opcache_invalidate( $real_file, true );

if ( ! isset( $result['message'] ) ) {
$message = __( 'Something went wrong.' );
} else {
$message = $result['message'];
unset( $result['message'] );
}

return new WP_Error( 'php_error', $message, $result );
}
}

注意:

这个方法仅仅是一个临时替代方式,因为如果wordpress程序更新时的wp-admin内的文件都会被替换掉。

错误原因:

1、wordpress主题和程序版本不兼容问题,有的主题修改是正常的,有主题php修改出现异常。

2、服务器系统与wordpress程序不兼容,比如楚狂人在阿里云的wordpress程序后台编辑正常,而在另一家服务器平台的就无法编辑。

不过这个算不了大问题,大家可以通过自己动手解决掉。

赞(0) 领红包
转载注明:楚狂人 » wordpress未能与站点联系来检查致命错误,因此PHP修改已被回滚

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

云计算我们是认真的

阿里云服务器腾讯云服务器

扫码领红包 或者 支付宝搜索:5981405

支付宝扫一扫打赏