ワードプレスの記事中にVine動画(VineのURL)を埋め込んで投稿したとき時、アイキャッチ画像にVineのサムネイルを自動で登録する方法です。
以下のコードをfunctons.phpに貼り付けてください。
記事中にvineのURLを見つけたらサムネイルをアイキャッチに登録
if (!function_exists('set_vine_thumbnail')) :
/**
* vineのURLを見つけたらサムネイルをアイキャッチに登録する
* @param type $post
* @throws Exception
*/
function set_vine_thumbnail($post)
{
// アイキャッチ画像が登録済みなら終了
if (has_post_thumbnail($post->ID))
return;
if (preg_match('#https?://vine\.co/v/([a-z0-9]+)\/?#i', $post->post_content, $matches)) {
$vine_id = $matches[1];
$url = sprintf("https://vine.co/oembed.json?id=%s", $vine_id);
// コンテンツを取得
$response = wp_remote_get($url);
if (is_wp_error($response))
throw new Exception(sprintf('%s', $response->get_error_message()));
if ($response['response']['code'] !== 200)
throw new Exception(sprintf('HTTPステータスコード:%s', $response['response']['code']));
$vine = json_decode($response['body']);
$vine->id = $vine_id;
// アップロードディレクトリ(パス名)を取得
$wp_upload_dir = wp_upload_dir();
if($wp_upload_dir['error'] !== false)
throw new Exception(sprintf('%s', $wp_upload_dir['error']));
$file_data = @file_get_contents($vine->thumbnail_url);
if (!$file_data)
throw new Exception(sprintf('%s', "画像が取得できませんでした。"));
$filename = basename(parse_url($vine->thumbnail_url, PHP_URL_PATH));
$wp_filetype = wp_check_filetype($filename, null);
$new_file = sprintf("%s/%s", $wp_upload_dir['path'], $filename);
file_put_contents($new_file, $file_data);
$attachment = array(
'guid' => sprintf("%s/%s", $wp_upload_dir['url'], $filename),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit',
);
// メディアライブラリに添付ファイルを挿入
$attach_id = wp_insert_attachment($attachment, $filename, $post->ID);
if (!$attach_id)
throw new Exception(sprintf('%s', "メディアライブラリに添付ファイルを挿入できませんでした。"));
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// 画像添付ファイルのメタデータを生成
$attach_data = wp_generate_attachment_metadata($attach_id, $new_file);
if (empty($attach_data))
throw new Exception(sprintf('%s', "画像添付ファイルのメタデータを生成"));
// 画像添付ファイルのメタデータを更新
if (wp_update_attachment_metadata($attach_id, $attach_data) === false)
throw new Exception(sprintf('%s', "画像添付ファイルのメタデータを更新できませんでした。"));
// 画像添付ファイルのファイルパスを更新
if (update_attached_file($attach_id, $new_file) === false)
throw new Exception(sprintf('%s', "画像添付ファイルのファイルパスを更新できませんでした。"));
// アイキャッチ画像を設定
if (set_post_thumbnail($post->ID, $attach_id) === false)
throw new Exception(sprintf('%s', "アイキャッチ画像を設定できませんでした。"));
}
}
endif;
公開ステータスに変更された時の処理
公開ステータスに変更された時のみアイキャッチ画像が登録されるように制限します。
if (!function_exists('custom_transition_post_status')) :
/**
* 公開ステータスに変更された時の処理
* @param type $new_status
* @param type $old_status
* @param type $post
*/
function custom_transition_post_status($new_status, $old_status, $post)
{
try {
if ($post->post_type == 'post' && $new_status == 'publish') {
switch ($old_status) {
case 'new': // 前のステータスがないとき
case 'draft': // 下書き状態の投稿
case 'pending': // レビュー待ちの投稿
case 'auto-draft': // 新しく作成された内容がない投稿
case 'future': // 公開予約された投稿
set_vine_thumbnail($post);
break;
case 'private': // 非公開の投稿
break;
case 'publish': // 公開された投稿やページ
break;
}
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
}
add_action('transition_post_status', 'custom_transition_post_status', 10, 3);
endif;
