Last active
February 23, 2025 06:01
-
-
Save holly/ddcffa1050bf657207996b523d2b9555 to your computer and use it in GitHub Desktop.
wordpress snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('xmlrpc_enabled', '__return_false'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('the_generator', '__return_empty_string'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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