2020年7月16日

wordpress/php さまざまなエラーと対処

getimagesize で429 エラー

getimagesizeで429 Too Many Requests エラーが出るときの対処

悪い例

httpから指定する

$imagesize = getimagesize('https://domain.com/images/sample.jpg');

良い例

サーバーのルートから指定する
$imagesize = getimagesize('var/www/domain.com/public_html/images/sample.png');

URL文字列をサーバールートからパス置き換える

$eyecatchimgには画像URL
$root_path = $_SERVER['DOCUMENT_ROOT'];
$pattern = '/https:\/\/domain.com/';
$root_img = preg_replace($pattern , $root_path , $eyecatchimg);

$imagesize = getimagesize($root_img);

記事内の画像をアイキャッチとして使うコード【エラー対策済み】

ネットに出回るコードをそのまま使うと出るエラーメッセージ

Notice: Undefined offset: 0 in [URL]functions.php on line

functions.php

function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    if (preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)){
        $first_img = $matches [1] [0];
    }else{
        $first_img = get_stylesheet_directory_uri().'/images/noimage.jpg';
    }
return $first_img;
}
参考:https://lib.ridesign.jp/php/undefined-offset-0/
Wish I could be Comic Sans ついにマイコンピューターでインターネットを始めたの
わたしはヨガマットデザイナー

fzfeed