轻量wordpress静态生成html插件源码分享
作者:xlnxin发布时间:2026-09-28分类:WordPress教程浏览:330
导读:wp-content/plugins/simple-static-html/simple-static-html.php<?php/** * Pl...
wp-content/plugins/simple-static-html/simple-static-html.php
<?php
/**
* Plugin Name: Simple Static HTML
* Description: 为文章、页面和首页生成静态 HTML 文件
* Version: 1.0.0
* Author: Your Name
* License: GPL-2.0+
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Simple_Static_HTML {
private $cache_dir;
public function __construct() {
// 缓存目录放在 wp-content/uploads/static-html/
$upload_dir = wp_upload_dir();
$this->cache_dir = rtrim( $upload_dir['basedir'], '/' ) . '/static-html';
add_action( 'init', array( $this, 'maybe_serve_static' ) );
add_action( 'save_post', array( $this, 'clear_related_cache' ), 20, 2 );
add_action( 'admin_post_simple_static_generate_all', array( $this, 'handle_generate_all' ) );
add_action( 'admin_menu', array( $this, 'add_admin_page' ) );
}
/**
* 未登录用户访问 GET 请求时,尝试直接输出静态 HTML
*/
public function maybe_serve_static() {
if ( is_admin() ) {
return;
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if ( isset( $_SERVER['REQUEST_METHOD'] ) && strtoupper( $_SERVER['REQUEST_METHOD'] ) !== 'GET' ) {
return;
}
if ( is_user_logged_in() ) {
return;
}
$path = $this->get_cache_path();
if ( $path && file_exists( $path ) ) {
header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
readfile( $path );
exit;
}
}
/**
* 文章/页面发布或更新后,重新生成对应静态文件
*/
public function clear_related_cache( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( $post->post_status !== 'publish' ) {
return;
}
$post_type = get_post_type( $post_id );
if ( ! in_array( $post_type, array( 'post', 'page' ), true ) ) {
return;
}
$this->generate_url( get_permalink( $post_id ) );
$this->generate_home();
}
/**
* 后台一键生成全站静态文件
*/
public function handle_generate_all() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( 'No permission.' );
}
check_admin_referer( 'simple_static_generate_all' );
$this->generate_home();
$posts = get_posts( array(
'post_type' => array( 'post', 'page' ),
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
) );
foreach ( $posts as $post_id ) {
$url = get_permalink( $post_id );
if ( $url ) {
$this->generate_url( $url );
}
}
wp_redirect( admin_url( 'tools.php?page=simple-static-html&generated=1' ) );
exit;
}
/**
* 添加后台工具页
*/
public function add_admin_page() {
add_management_page(
'Static HTML',
'Static HTML',
'manage_options',
'simple-static-html',
array( $this, 'render_admin_page' )
);
}
public function render_admin_page() {
?>
<div class="wrap">
<h1>生成静态 HTML</h1>
<p>点击下方按钮,为首页、文章和页面生成静态 HTML 文件。</p>
<?php if ( ! empty( $_GET['generated'] ) ) : ?>
<div class="notice notice-success"><p>静态文件已生成。</p></div>
<?php endif; ?>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
<input type="hidden" name="action" value="simple_static_generate_all" />
<?php wp_nonce_field( 'simple_static_generate_all' ); ?>
<?php submit_button( '生成全部静态文件' ); ?>
</form>
<h2>缓存目录</h2>
<p><code><?php echo esc_html( $this->cache_dir ); ?></code></p>
<p>请确保该目录可写。</p>
</div>
<?php
}
/**
* 生成指定 URL 的静态 HTML
*/
private function generate_url( $url ) {
$url = esc_url_raw( $url );
if ( ! $url ) {
return;
}
$response = wp_remote_get( $url, array(
'timeout' => 30,
'redirection' => 5,
'sslverify' => false,
) );
if ( is_wp_error( $response ) ) {
return;
}
$code = wp_remote_retrieve_response_code( $response );
if ( $code !== 200 ) {
return;
}
$body = wp_remote_retrieve_body( $response );
if ( ! $body ) {
return;
}
$path = $this->url_to_cache_path( $url );
if ( ! $path ) {
return;
}
$dir = dirname( $path );
if ( ! is_dir( $dir ) ) {
wp_mkdir_p( $dir );
}
file_put_contents( $path, $body );
}
/**
* 生成首页静态文件
*/
private function generate_home() {
$this->generate_url( home_url( '/' ) );
}
/**
* 把 URL 转成缓存文件路径
*/
private function url_to_cache_path( $url ) {
$home = home_url( '/' );
if ( strpos( $url, $home ) !== 0 ) {
return '';
}
$relative = substr( $url, strlen( $home ) );
$relative = trim( $relative, '/' );
if ( $relative === '' ) {
$relative = 'index';
}
// 去掉查询参数
$pos = strpos( $relative, '?' );
if ( $pos !== false ) {
$relative = substr( $relative, 0, $pos );
}
$relative = trim( $relative, '/' );
if ( $relative === '' ) {
$relative = 'index';
}
return $this->cache_dir . '/' . $relative . '/index.html';
}
/**
* 获取当前请求对应的缓存文件路径
*/
private function get_cache_path() {
$scheme = is_ssl() ? 'https://' : 'http://';
$host = isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : '';
$uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/';
$url = $scheme . $host . $uri;
return $this->url_to_cache_path( $url );
}
}
new Simple_Static_HTML();
- WordPress教程排行
- 最近发表
