BackWPup备份报错
BackWPup备份提示 弃用:Requests_Cookie_Jar::offsetExists($key) 的返回类型应与 ArrayAccess::offsetExists(mixed $offset): bool 兼容,或者应使用 #[\ReturnTypeWillChange] 属性来暂时禁止通知
这个错误提示是因为 BackWPup 使用的 Requests 库与 PHP 版本不兼容,尤其是在 PHP 8.1 及以上版本,由于
Requests_Cookie_Jar
类中的offsetExists()
方法没有正确声明返回类型,因此触发了弃用(Deprecated)警告。
🔥 解决方案
✅ 方法 1:更新 BackWPup 插件(推荐)
- 登录 WordPress 后台。
- 进入 "插件" → "已安装的插件"。
- 找到 BackWPup,点击 "检查更新" 并更新到最新版本。
- 如果更新后仍然报错,尝试 禁用并重新启用插件。
🔹 为什么?
最新版本的 BackWPup 可能已经修复了 PHP 8.1+ 兼容性问题。
✅ 方法 2:降级 PHP 版本(临时方案)
如果你的服务器 运行的是 PHP 8.1 或 8.2,可以尝试切换到 PHP 7.4 或 8.0(兼容性更好):
- 进入 服务器控制面板(如 cPanel、Plesk 或 DirectAdmin)。
- 找到 PHP 版本管理,选择 PHP 7.4 或 8.0。
- 保存更改,然后刷新网站并测试 BackWPup 是否正常工作。
✅ 方法 3:手动修改 BackWPup 源码(高级用户)
如果你不想降级 PHP,可以手动修改 Requests_Cookie_Jar.php
代码来绕过这个问题:
-
通过 FTP 或 cPanel 进入 WordPress 目录。
-
找到 BackWPup 插件文件:
wp-content/plugins/backwpup/vendor/rmccue/requests/library/Requests/Cookie/Jar.php #或者以下路径,需要修改的是下面两个路径。 wp-includes/Requests/Cookie/Jar.php your_wordpress_root/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php
-
修改后jar.php代码(兼容 PHP 8.1+):
<?php /** * Cookie holder object * * @package Requests * @subpackage Cookies */ /** * Cookie holder object * * @package Requests * @subpackage Cookies */ class Requests_Cookie_Jar implements ArrayAccess, IteratorAggregate { /** * Actual item data * * @var array */ protected $cookies = array(); /** * Create a new jar * * @param array $cookies Existing cookie values */ public function __construct($cookies = array()) { $this->cookies = $cookies; } /** * Normalise cookie data into a Requests_Cookie * * @param string|Requests_Cookie $cookie * @return Requests_Cookie */ public function normalize_cookie($cookie, $key = null) { if ($cookie instanceof Requests_Cookie) { return $cookie; } return Requests_Cookie::parse($cookie, $key); } /** * Normalise cookie data into a Requests_Cookie * * @codeCoverageIgnore * @deprecated Use {@see Requests_Cookie_Jar::normalize_cookie} * @return Requests_Cookie */ public function normalizeCookie($cookie, $key = null) { return $this->normalize_cookie($cookie, $key); } /** * Check if the given item exists * * @param string $key Item key * @return boolean Does the item exist? */ /*public function offsetExists($key) { return isset($this->cookies[$key]); }*/ public function offsetExists($key): bool { return isset($this->cookies[$key]); } /** * Get the value for the item * * @param string $key Item key * @return string|null Item value (null if offsetExists is false) */ public function offsetGet($key): mixed { if (!isset($this->cookies[$key])) { return null; } return $this->cookies[$key]; } /** * Set the given item * * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) * * @param string $key Item name * @param string $value Item value */ public function offsetSet($key, $value): void { if ($key === null) { throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset'); } $this->cookies[$key] = $value; } /** * Unset the given header * * @param string $key */ public function offsetUnset($key): void { unset($this->cookies[$key]); } /** * Get an iterator for the data * * @return ArrayIterator */ public function getIterator(): Traversable { return new ArrayIterator($this->cookies); } /** * Register the cookie handler with the request's hooking system * * @param Requests_Hooker $hooks Hooking system */ public function register(Requests_Hooker $hooks) { $hooks->register('requests.before_request', array($this, 'before_request')); $hooks->register('requests.before_redirect_check', array($this, 'before_redirect_check')); } /** * Add Cookie header to a request if we have any * * As per RFC 6265, cookies are separated by '; ' * * @param string $url * @param array $headers * @param array $data * @param string $type * @param array $options */ public function before_request($url, &$headers, &$data, &$type, &$options) { if (!$url instanceof Requests_IRI) { $url = new Requests_IRI($url); } if (!empty($this->cookies)) { $cookies = array(); foreach ($this->cookies as $key => $cookie) { $cookie = $this->normalize_cookie($cookie, $key); // Skip expired cookies if ($cookie->is_expired()) { continue; } if ($cookie->domain_matches($url->host)) { $cookies[] = $cookie->format_for_header(); } } $headers['Cookie'] = implode('; ', $cookies); } } /** * Parse all cookies from a response and attach them to the response * * @var Requests_Response $response */ public function before_redirect_check(Requests_Response $return) { $url = $return->url; if (!$url instanceof Requests_IRI) { $url = new Requests_IRI($url); } $cookies = Requests_Cookie::parse_from_headers($return->headers, $url); $this->cookies = array_merge($this->cookies, $cookies); $return->cookies = $this; } }
-
修改后CaseInsensitiveDictionary.php代码(兼容 PHP 8.1+):
<?php
/**
* Case-insensitive dictionary, suitable for HTTP headers
*
* @package Requests
* @subpackage Utilities
*/
/**
* Case-insensitive dictionary, suitable for HTTP headers
*
* @package Requests
* @subpackage Utilities
*/
class Requests_Utility_CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate {
/**
* Actual item data
*
* @var array
*/
protected $data = array();
/**
* Creates a case insensitive dictionary.
*
* @param array $data Dictionary/map to convert to case-insensitive
*/
public function __construct(array $data = array()) {
foreach ($data as $key => $value) {
$this->offsetSet($key, $value);
}
}
/**
* Check if the given item exists
*
* @param string $key Item key
* @return boolean Does the item exist?
*/
public function offsetExists($key): bool {
$key = strtolower($key);
return isset($this->data[$key]);
}
public function offsetGet($key): mixed {
$key = strtolower($key);
if (!isset($this->data[$key])) {
return null;
}
return $this->data[$key];
}
/**
* Set the given item
*
* @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
*
* @param string $key Item name
* @param string $value Item value
*/
public function offsetSet($key, $value): void {
if ($key === null) {
throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
}
$key = strtolower($key);
$this->data[$key] = $value;
}
/**
* Unset the given header
*
* @param string $key
*/
public function offsetUnset($key): void {
unset($this->data[strtolower($key)]);
}
/**
* Get an iterator for the data
*
* @return ArrayIterator
*/
public function getIterator(): Traversable {
return new ArrayIterator($this->data);
}
/**
* Get the headers as an array
*
* @return array Header data
*/
public function getAll(): array {
return $this->data;
}
}
此时再进行备份就不会报错啦。
- 保存修改,重新上传文件,然后刷新 WordPress 备份页面。
🚀 结论
- 方法 1(更新插件) 👉 最推荐的方式。
- 方法 2(降级 PHP 版本) 👉 适用于插件未更新但你需要尽快解决问题的情况。
- 方法 3(手动修改代码) 👉 适用于懂代码的用户,或者插件作者未修复时的临时方案。