迷你日志主题十分简洁,一旦切换其他的主题设置就没了,为迷你日志主题添加主题备份和恢复功能。
一、确定文件备份的路径
在当前主题目录下建立 tmp 目录,赋予可写权限777,linux 系统赋权命令如下: chmod 777 -R tmp/
二、在主题 funcitons.php 文件的 themeConfig($form){} 方法中添加编写代码
通常将备份恢复的模块放在网页的最前面,将下面的代码,复制到themeConfig($form){}最前面就可以。
// ========== 主题配置备份和恢复功能 ==========
$theTheme = Helper::options()->theme;
$db = Typecho_Db::get();
// 备份目录
$backupDir = __DIR__ . '/tmp/';
if (!is_dir($backupDir)) {
@mkdir($backupDir, 0755, true);
}
$backPath = $backupDir . $theTheme . '.txt';
// 获取主题配置
$themeConfigRow = $db->fetchRow($db->select('value')->from('table.options')->where('name = ?', 'theme:' . $theTheme));
$themeConfStr = $themeConfigRow ? $themeConfigRow['value'] : '';
// 读取备份
$backstr = file_exists($backPath) ? @file_get_contents($backPath) : '';
$hasBackup = !empty($backstr);
// 处理 POST 请求 - 放在最前面,不输出任何HTML
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 备份
if (isset($_POST['backup'])) {
$result = @file_put_contents($backPath, $themeConfStr);
if ($result !== false) {
echo '<script>alert("备份成功!");setTimeout(function(){window.location.href=window.location.href;}, 500);</script>';
} else {
echo '<script>alert("备份失败,请检查tmp目录权限");</script>';
}
return;
}
// 恢复
if (isset($_POST['restore']) && $hasBackup) {
$str = @file_get_contents($backPath);
if ($str !== false && !empty($str)) {
try {
$updateQuery = $db->update('table.options')->rows(['value' => $str])->where('name = ?', 'theme:' . $theTheme);
$result = $db->query($updateQuery);
if ($result !== false) {
if (isset($GLOBALS['___typecho_options'])) {
unset($GLOBALS['___typecho_options']);
}
echo '<script>alert("恢复成功!");setTimeout(function(){window.location.href=window.location.href;}, 500);</script>';
} else {
echo '<script>alert("恢复失败");</script>';
}
} catch (Exception $e) {
echo '<script>alert("恢复失败:' . addslashes($e->getMessage()) . '");</script>';
}
} else {
echo '<script>alert("备份文件为空或损坏");</script>';
}
return;
}
}
// 显示备份界面
?>
<style>
/* 淡灰色线框 + 简约灰色按钮 */
.theme-backup-box {
max-width: 780px;
margin: 20px 0;
padding: 22px 24px;
border: 1px solid #dcdfe6;
border-radius: 8px;
background: #ffffff;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
.theme-backup-title {
font-size: 16px;
font-weight: 500;
color: #333;
margin-bottom: 18px;
}
/* 状态提示 */
.theme-backup-status {
padding: 12px 16px;
border-radius: 6px;
margin-bottom: 18px;
font-size: 14px;
line-height: 1.5;
border: 1px solid #e4e7ed;
background: #fafbfc;
color: #666;
}
.theme-backup-status.status-error {
border-color: #f5d6d6;
background: #fdf3f3;
color: #b95050;
}
.theme-backup-status.status-consistent {
border-color: #d6e4ff;
background: #f2f5ff;
color: #4e5f77;
}
.theme-backup-status.status-inconsistent {
border-color: #ffe9c1;
background: #fffaf2;
color: #9a6f2d;
}
/* 按钮组 */
.theme-backup-buttons {
display: flex;
gap: 12px;
margin-bottom: 18px;
flex-wrap: wrap;
}
/* 灰色系按钮 */
.backup-btn {
padding: 8px 18px;
border-radius: 6px;
font-size: 14px;
cursor: pointer;
transition: all 0.2s ease;
border: 1px solid #dcdfe6;
background: #f5f7fa;
color: #444;
}
.backup-btn:hover {
background: #e9ebf0;
border-color: #c8c9cc;
}
/* 信息栏 */
.backup-info {
font-size: 12px;
color: #909399;
padding: 8px 12px;
background: #f7f8fa;
border-radius: 6px;
border: 1px solid #e4e7ed;
}
</style>
<div class="theme-backup-box">
<div class="theme-backup-title">主题配置备份与恢复</div>
<?php if (!is_writable($backupDir)): ?>
<div class="theme-backup-status status-error">
警告:tmp 目录不可写!请设置权限为 755 或 777
</div>
<?php endif; ?>
<div class="theme-backup-status <?php
if (!$hasBackup) echo 'status-error';
elseif (strcmp($backstr, $themeConfStr) === 0) echo 'status-consistent';
else echo 'status-inconsistent';
?>">
<?php if (!$hasBackup): ?>
暂无备份文件,请先创建备份
<?php elseif (strcmp($backstr, $themeConfStr) === 0): ?>
当前配置与备份一致,无需操作
<?php else: ?>
当前配置与备份不一致,建议备份或恢复
<?php endif; ?>
<?php if ($hasBackup): ?>
<div style="margin-top:6px; font-size:12px; color:#666;">
备份时间:<?php echo date('Y-m-d H:i:s', filemtime($backPath)); ?>
</div>
<?php endif; ?>
</div>
<form method="post">
<div class="theme-backup-buttons">
<button type="button" class="backup-btn" onclick="doBackup()">备份配置</button>
<?php if ($hasBackup): ?>
<button type="button" class="backup-btn" onclick="doRestore()">恢复配置</button>
<?php endif; ?>
</div>
</form>
<div class="backup-info">
备份位置:tmp/<?php echo $theTheme; ?>.txt
</div>
</div>
<script>
function doBackup() {
if (confirm('确定要备份当前主题配置吗?')) {
var form = document.createElement('form');
form.method = 'POST';
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'backup';
input.value = '1';
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
}
function doRestore() {
if (confirm('警告:恢复操作会覆盖当前所有主题设置!\n\n确定要继续吗?')) {
var form = document.createElement('form');
form.method = 'POST';
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'restore';
input.value = '1';
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
}
</script>
<?php
// 主题配置恢复备份--结束
本文著作权归作者 [ 老罗 ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
tmp目录我记得系统重启后会自动清空,存储临时文件最好啊。这个主题真是快,极速打开。👍
还好目前我还没遇到tem目录被自动清空的情况
嗯?我怎么记得typecho也可以直接备份数据库?本地文件似乎没咋变吧?
是啊,typecho后台有备份数据库的功能,这个是给模板的设置来备份的。
这点还是WP好,主题随便切换,配置都在数据库,可能这个其中之一的原因让WP变得相对臃肿了吧
嗯这个tp比较简单。
我也买了typecho版本的这个主题,轻巧好用。
所以要不要加入typecho?😄
才转到 WordPress 不久
几年前看上了handsome那个主题,从zblog转到typecho
迷你主题还是挺不错的
是不错,再完善一些细节会更好