Created
May 11, 2014 13:22
-
-
Save jz5/a2eb31f42a014ce6a9b6 to your computer and use it in GitHub Desktop.
WordPress plugin: Post from photo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment