Written by Anonymous
// ダッシュボードウィジェットを登録
add_action('wp_dashboard_setup', function() {
wp_add_dashboard_widget(
'debug_log_widget',
'Debug Log ビューアー',
'render_debug_log_widget'
);
});
/**
* 管理画面のダッシュボードウィジェットを表示
*/
if (!function_exists('render_debug_log_widget')):
function render_debug_log_widget() {
$log_file = WP_CONTENT_DIR . '/debug.log'; // ログファイルの絶対パス
$log_url = content_url('/debug.log'); // ログファイルURL
?>
<div id="log-widget-container">
<?php if (file_exists($log_file)): ?>
<iframe src="<?php echo esc_url($log_url); ?>" style="width:100%; height:200px; border:1px solid #ccc;"></iframe>
<?php else : ?>
<p>現在、debug.logは存在しません。</p>
<?php endif; ?>
<button id="delete-log-btn" class="button button-primary">ログを削除</button>
</div>
<script>
jQuery(function($) {
$("#delete-log-btn").on("click", function() {
if(!confirm("本当にdebug.logを削除しますか?")) {
return;
}
$.post(ajaxurl, {
action: "delete_debug_log"
}, function(response) {
alert(response);
location.reload();
});
});
});
</script>
<?php
}
endif;
// ログファイルを削除
add_action('wp_ajax_delete_debug_log', function() {
if (!current_user_can('manage_options')) {
wp_die('権限がありません');
}
$log_file = WP_CONTENT_DIR . '/debug.log';
// ファイルが存在する場合
if (file_exists($log_file)) {
if (unlink($log_file)) {
echo 'debug.logを削除しました。';
} else {
echo 'ファイルの削除に失敗しました。パーミッションを確認してください。';
}
} else {
echo 'ファイルは既に存在しません。';
}
wp_die();
});