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);
wordpress
記事内の画像をアイキャッチとして使うコード【エラー対策済み】
ネットに出回るコードをそのまま使うと出るエラーメッセージ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;
}
Wordpressの外でデータを読み込むとnetwork statusが404になる
404がでる悪い例
require_once ('../wp-blog-header.php');
良い例
require_once ('../wp-load.php');