写真から投稿を作成したいのだけど、すぐ良さそうなプラグインが見つからなかったので少しコードを書いた。まだ、全然使いやすくないが公開しておく。
管理画面から写真を選択してアップロードすると、次のことを行う。
- 新規記事を作成(ファイル名をタイトル)
- メディアへ写真を登録
- 記事へ写真を挿入と投稿日時を撮影日時に変更して更新
少し新規投稿がしやすくなるライブラリを利用させてもらった。
理想は、写真をドラッグ & ドロップすると投稿がどんどんできる動作。WordPress メディアの登録から投稿を作成したり、WordPress の機能のメディアの登録処理に追加するアプローチの方が良いかもしれない。
画面
コード
EXIF 情報のある大き目の写真を登録してほぼエラーが発生しない前提。
インストールは、下記コードのファイルと、wokamoto/wp_post_helper を /wp-content/plugins/post-from-photo ディレクトリに入れる。
<?php | |
/* | |
Plugin Name: Post from photo | |
*/ | |
if (!isset($_SESSION)) { | |
session_start(); | |
} | |
require_once(ABSPATH . 'wp-load.php'); | |
require_once(dirname( __FILE__ ) . '/class-wp_post_helper.php'); // https://github.com/wokamoto/wp_post_helper | |
add_action('admin_menu', 'post_from_photo_plugin_menu'); | |
function post_from_photo_plugin_menu() { | |
add_menu_page('Post from photo', 'Post from photo', 'administrator', __FILE__, 'post_from_photo_plugin_main'); | |
} | |
function post_from_photo_plugin_main() { | |
if (isset($_POST["token"]) && | |
isset($_SESSION['post_from_photo_plugin_token']) && | |
$_POST["token"] === $_SESSION['post_from_photo_plugin_token']) { | |
if ($image_name = post_from_photo_insert_post()) { | |
$msg = "<p>Posted: $image_name</p>"; | |
} | |
} | |
$token = uniqid(); | |
$_SESSION['post_from_photo_plugin_token'] = $token; | |
echo <<< EOM | |
<h2>Post from photo</h2> | |
$msg | |
<div> | |
<form name="pfp_form" method="post" action="" method="post" enctype="multipart/form-data"> | |
<input type="hidden" value="$token" name="token" /> | |
<input name="file" type="file" /> | |
<input type="submit" value="Post" /> | |
</form> | |
</div> | |
EOM; | |
} | |
function post_from_photo_insert_post() { | |
// | |
// 写真アップロード | |
// | |
$upload_dir = wp_upload_dir(); | |
$file_dir = trailingslashit($upload_dir['path']); | |
// make directory | |
if (!file_exists($file_dir)) { | |
$dirs = explode('/', $file_dir); | |
$subdir = '/'; | |
foreach ($dirs as $dir) { | |
if (!empty($dir)) { | |
$subdir .= $dir . '/'; | |
if (!file_exists($subdir)) { | |
mkdir($subdir); | |
} | |
} | |
} | |
} | |
$image_name = $_FILES["file"]["name"]; | |
$image_file = $file_dir . $_FILES["file"]["name"]; | |
if (!move_uploaded_file($_FILES['file']['tmp_name'], $image_file)) { | |
return false; | |
} | |
// | |
// 投稿 | |
// | |
$post = new wp_post_helper(array( | |
'post_name' => $image_name, | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'post_title' => $image_name, | |
'post_content' => '', | |
)); | |
if (!$post->insert()) { | |
return false; | |
} | |
// メディア追加 | |
$attach_id = $post->add_media( | |
$image_file, // メディアファイルの絶対パス | |
$image_name, // メディアの「タイトル」 | |
'', // メディアの「説明」 | |
'', // メディアの「キャプション」 | |
false // true (アイキャッチ画像にする) or false (アイキャッチ画像にしない) | |
); | |
// | |
// 写真を挿入と投稿日時の変更 | |
// | |
$a = wp_generate_attachment_metadata($attach_id, $image_file); | |
// リンク先画像 URL | |
$image_url = $upload_dir['url'] . '/' . basename($a['file']); | |
// 表示画像 URL | |
if (array_key_exists('large', $a['sizes'])) { | |
$thumb_url = $upload_dir['url'] . '/' . $a['sizes']['large']['file']; | |
} else if (array_key_exists('medium', $a['sizes'])) { | |
$thumb_url = $upload_dir['url'] . '/' . $a['sizes']['medium']['file']; | |
} | |
// 本文 | |
$content = '<a href="' . $image_url . '"><img src="' . $thumb_url . '" class="alignnone size-large wp-image-' . $attach_id . '" /></a>'; | |
// 更新 | |
// MEMO: draft に対して post_date の変更は無効 | |
$post->set(array( | |
'post_date' => date("Y-m-d H:i:s", $a['image_meta']['created_timestamp']), // 投稿日時を撮影日時へ変更 | |
'post_content' => $content, | |
)); | |
if ($postid = $post->update()) { | |
// 投稿フォーマット変更 | |
set_post_format($postid, 'image'); | |
} | |
unset($post); | |
return $image_name; | |
} | |
?> |
wp_update_post で post_date を更新
少しはまったのが、draft で新規投稿し、publish で投稿を更新すると、更新時に投稿日時(post_date)の変更が有効にならないみたい。publish → publish だとうまく動作する。