Skip to content

Instantly share code, notes, and snippets.

@holly
Last active February 23, 2025 06:01
Show Gist options
  • Save holly/ddcffa1050bf657207996b523d2b9555 to your computer and use it in GitHub Desktop.
Save holly/ddcffa1050bf657207996b523d2b9555 to your computer and use it in GitHub Desktop.
wordpress snippets
<?php
function add_security_headers( $headers ) {
$headers['Referrer-Policy'] = 'no-referrer';
$headers['X-Permitted-Cross-Domain-Policies'] = 'none';
$headers['X-Download-Options'] = 'noopen';
$headers['X-Frame-Options'] = 'SAMEORIGIN';
$headers['X-Content-Type-Options'] = 'nosniff';
// CSP (Content-Security-Policy)
$headers['Content-Security-Policy'] = "default-src 'self'; base-uri 'self'; block-all-mixed-content; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data:; object-src 'none'; script-src 'self'; script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; upgrade-insecure-requests";
return $headers;
}
add_filter( 'wp_headers', 'add_security_headers' );
<?php
add_filter('xmlrpc_enabled', '__return_false');
<?php
/**
* アップロードされた画像からWebP画像を生成する関数
*
* @param string $file_path 変換対象の画像ファイルパス
* @return mixed 変換後の情報、または false(エラー時)
*/
function convert_image_to_webp( $file_path ) {
if ( ! file_exists( $file_path ) ) {
return false;
}
// 画像エディタを取得(GDまたはImagick)
$editor = wp_get_image_editor( $file_path );
if ( is_wp_error( $editor ) ) {
return false;
}
// WebP用の出力パス(拡張子をwebpに変更)
//$dir = pathinfo( $file_path, PATHINFO_DIRNAME );
//$filename = pathinfo( $file_path, PATHINFO_FILENAME );
//$dest_path = trailingslashit( $dir ) . $filename . '.webp';
$dest_path = $file_path . ".webp";
// 出力フォーマットをWebPに設定して保存
$result = $editor->save( $dest_path, 'image/webp' );
if ( is_wp_error( $result ) ) {
return false;
}
return $result;
}
/**
* 画像アップロード後、オリジナルおよび各サイズ画像のWebP版を生成する処理
*
* @param array $metadata 添付ファイルの画像メタデータ
* @param int $attachment_id 添付ファイルID
* @return array 修正(またはそのまま返す)されたメタデータ
*/
function generate_webp_on_upload( $metadata, $attachment_id ) {
// 添付ファイルのパスを取得
$file_path = get_attached_file( $attachment_id );
// JPEG, JPG, PNG 以外は対象外
$ext = strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) );
if ( ! in_array( $ext, array( 'jpeg', 'jpg', 'png' ), true ) ) {
return $metadata;
}
// オリジナル画像のWebP版を生成
convert_image_to_webp( $file_path );
// 各サイズ画像についてWebP版を生成
if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) {
$upload_dir = pathinfo( $file_path, PATHINFO_DIRNAME );
foreach ( $metadata['sizes'] as $size => $size_data ) {
$resized_file = trailingslashit( $upload_dir ) . $size_data['file'];
convert_image_to_webp( $resized_file );
}
}
return $metadata;
}
/**
* オリジナル画像削除時に対応するWebPファイルを削除する関数
*
* @param int $attachment_id 削除対象の添付ファイルID
*/
function delete_webp_files_on_delete( $attachment_id ) {
// オリジナル画像のパスを取得
$file = get_attached_file( $attachment_id );
if ( ! $file || ! file_exists( $file ) ) {
return;
}
// オリジナル画像に対応するWebPファイル(例: sample.jpg.webp)の削除
$webp_file = $file . '.webp';
if ( file_exists( $webp_file ) ) {
@unlink( $webp_file );
}
// 各サイズ画像のWebPファイルも削除する
$metadata = wp_get_attachment_metadata( $attachment_id );
if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) {
$upload_dir = pathinfo( $file, PATHINFO_DIRNAME );
foreach ( $metadata['sizes'] as $size => $size_data ) {
$resized_file = trailingslashit( $upload_dir ) . $size_data['file'];
$webp_resized = $resized_file . '.webp';
if ( file_exists( $webp_resized ) ) {
@unlink( $webp_resized );
}
}
}
}
add_filter( 'wp_generate_attachment_metadata', 'generate_webp_on_upload', 10, 2 );
add_action( 'delete_attachment', 'delete_webp_files_on_delete' );
<php
// REST API経由でユーザー名が特定されることを防ぐ
function my_filter_rest_endpoints( $endpoints ) {
if ( isset( $endpoints['/wp/v2/users'] ) ) {
unset( $endpoints['/wp/v2/users'] );
}
if ( isset( $endpoints['/wp/v2/users/(?P[\d]+)'] ) ) {
unset( $endpoints['/wp/v2/users/(?P[\d]+)'] );
}
return $endpoints;
}
add_filter( 'rest_endpoints', 'my_filter_rest_endpoints', 10, 1 );
<?php
function remove_query_strings() {
if(!is_admin()) {
add_filter('script_loader_src', 'remove_query_strings_split', 15);
add_filter('style_loader_src', 'remove_query_strings_split', 15);
}
}
function remove_query_strings_split($src){
$output = preg_split("/(&ver|\?ver)/", $src);
return $output[0];
}
add_action('init', 'remove_query_strings');
<?php
add_filter('the_generator', '__return_empty_string');
<?php
// ユーザー名の特定を防ぐ
function theme_slug_redirect_author_archive() {
if (is_author() ) {
wp_redirect( home_url());
exit;
}
}
add_action( 'template_redirect', 'theme_slug_redirect_author_archive' );
<?php
add_filter( 'wp_image_editors', function( $editors ) {
return array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment