2017-01-20 15 views
0

私は専門家ではありませんが、WordPressでWeb画像を使用しようとしています。特定のディレクトリ内のファイル拡張子を置き換えます。

イメージをWeb形式に変換するRj Webp Converterというプラグインがあります。この機能を使用してjpg、jpeg、pngなどの拡張子をwebpに置き換えています。

function callback($buffer) { 
    if(!is_admin() && $GLOBALS['pagenow'] != 'wp-login.php') { 
     $buffer = str_replace('.jpg','.webp',$buffer); 
     $buffer = str_replace('.jpeg','.webp',$buffer); 
     $buffer = str_replace('.png','.webp',$buffer); } 
    return $buffer; } 
function buffer_start() { ob_start("callback"); } 
function buffer_end() { ob_end_flush(); } 
add_action('after_setup_theme', 'buffer_start'); 
add_action('shutdown', 'buffer_end'); 

しかし、1つの問題があります。 上記のコードはすべての画像ファイルを置き換えます。 wp-content/uploads/...ディレクトリ内のファイルのみを置き換えたいとします。

何か助けていただければ幸いです。 :)

答えて

0

これは動作するはずです。

REGEX

<?php 
$re = '/(.*(?<=\.)(?:jpg|jpeg|png)(?=$))/m'; 
$str = 'test.jpg 
testing.png 
test.jpeg'; 

preg_match_all($re, $str, $matches); 

// Print the entire match result 
print_r($matches); 
?> 

PHPあなたは$に$ファイル名を置き換える必要がありますforeachを使用してと一致します。

function replace_extension($filename, $new_extension) { 
    $info = pathinfo($filename); 
    return $info['filename'] . '.' . $new_extension; 
} 
関連する問題