インターネット上の画像をwordpressにプログラムでサムネイル登録できるようにする方法

指定したインターネット上の画像をいちいちローカルに保存してメディアにアップロードしてサムネ登録するのが非常にめんどくさかったので、コードを実装してみた

目次

画像ダウンロード関数

function func_downloadImage($url,$name){

//画像のパス
$image_path = $url;

//保存するファイル名
$file_name = $name.'_tmp.png';

$image = file_get_contents($image_path);

$save_path = dirname(__FILE__) . '/tmp/'.$file_name;
file_put_contents($save_path, $image);

}

file_get_contentsで指定の画像をtmpフォルダに保管する

tmpフォルダは書き込み可能な状態にしておくこと

 

画像アップロード関数

function func_imageUpload($filePath){
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');

if (file_exists($filePath)) {

// サイズ上限チェック
if (filesize($filePath) > 1000000) {
$create_thread_error[] = 'ファイルサイズが大きすぎます。';
}else{
//var_dump(filesize($filePath));
}

// getimagesizeを利用しMIMEタイプをチェック
$imageInfo = getimagesize($filePath);
list($orig_width, $orig_height, $image_type) = $imageInfo;
if ($imageInfo === false) {
$create_thread_error[] = '画像ファイルではありません。';
} else {
$ext = substr(basename($filePath), strrpos(basename($filePath), '.') + 1);
if (false === $ext = array_search(
$imageInfo['mime'],
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
$create_thread_error[] = '画像形式が未対応です。';
}
}

$user = wp_get_current_user();
$fileDestName = $user->get('user_login').'-'. date("YmdHis") .'.'. $ext;
$file_array = array(
'name' => $fileDestName,
'tmp_name' => $filePath,
);

// Post ID to attach upload to, 0 for none.
$post_id = 0;

$attachment_id = media_handle_sideload( $file_array, $post_id );

if ( ! is_wp_error( $attachment_id ) ) {
echo "アップロード完了 : ".$attachment_id." : ".$fileDestName."<br>";
return $attachment_id;

} else {
echo "Error";
print_r($attachment_id);
}

} else {
$create_thread_error[] = 'ファイルが選択されていません。';
}

if(isset($create_thread_error)){
var_dump($create_thread_error);
}
}

tmpフォルダに保管したファイルをwordpressのmedia_handle_sideload関数でメディアにアップロード。attachment_idを取得して、returnで返している。

wordpressにアップロードするファイル名は、ユーザ名+日付にしている。

ローカルファイルをWordpressにアップロードするのはmedia_handle_sideload関数を使う必要があるので注意

 

WordPressに記事投稿してサムネ登録

function func_postArticle($post,$thumbID,$customImageID){

$params = array(
'post_author' => get_current_user_id(), // ユーザID
'post_name' => 'information-'.rand(), // パーマリンク
'post_title' => $post['title'], // 投稿タイトル
'post_content' => $post['content'], // 投稿本文
'tags_input' => $post['tag'], // タグ名
'post_status' => 'publish',
'post_type' => 'information'
);
$id = wp_insert_post($params);
set_post_thumbnail( $id, $thumbID); //サムネイル設定

//カスタムフィールド
if($id){
update_post_meta($id, 'customImage', $customImageID);
}

}

すべてを結合すると下記のようになる

func_downloadImage("サムネ用画像URL","thumb");
func_downloadImage("カスタムフィールド用画像URL","customImage");
$thumbID = func_imageUpload("thumbのファイルパス");
$customImageID = func_imageUpload("customImageのファイルパス");

$post['title'] = "記事タイトル";
$post['content'] = "記事内容";
$post['tag'] = "タグ1,タグ2";

func_postArticle($post,$thumbID,$customImageID);//投稿

 

 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です