update_recently_edited()
云策文档标注
概述
update_recently_edited() 函数用于更新插件或主题文件编辑器中的“最近编辑”文件列表。它通过管理选项来维护一个最近编辑文件的记录,并限制列表长度。
关键要点
- 函数接受一个必需的字符串参数 $file,表示要添加的文件路径。
- 它从 get_option('recently_edited') 获取现有列表,将新文件添加到列表开头,并确保列表唯一且长度不超过5个条目。
- 使用 update_option() 保存更新后的列表,并返回更新后的数组。
代码示例
function update_recently_edited( $file ) {
$oldfiles = (array) get_option( 'recently_edited' );
if ( $oldfiles ) {
$oldfiles = array_reverse( $oldfiles );
$oldfiles[] = $file;
$oldfiles = array_reverse( $oldfiles );
$oldfiles = array_unique( $oldfiles );
if ( 5 < count( $oldfiles ) ) {
array_pop( $oldfiles );
}
} else {
$oldfiles = array( $file );
}
update_option( 'recently_edited', $oldfiles );
return $oldfiles;
}注意事项
- 此函数自 WordPress 1.5.0 版本引入,用于内部文件编辑器功能。
- 相关函数包括 update_option() 和 get_option(),用于选项的更新和检索。
原文内容
Updates the “recently-edited” file for the plugin or theme file editor.
Parameters
$filestringrequired
Source
function update_recently_edited( $file ) {
$oldfiles = (array) get_option( 'recently_edited' );
if ( $oldfiles ) {
$oldfiles = array_reverse( $oldfiles );
$oldfiles[] = $file;
$oldfiles = array_reverse( $oldfiles );
$oldfiles = array_unique( $oldfiles );
if ( 5 < count( $oldfiles ) ) {
array_pop( $oldfiles );
}
} else {
$oldfiles[] = $file;
}
update_option( 'recently_edited', $oldfiles );
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |