データ移行の時とか、ダッシュボード以外から投稿するプラグインを作るときに使ってね。
https://github.com/wokamoto/wp_post_helper
簡単な使用方法
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 | |
require_once('/path/to/wordpress/wp-load.php'); | |
require_once('class-wp_post_helper.php'); | |
// 初期化 | |
$post = new wp_post_helper(array( | |
'post_name' => 'slug' , // スラッグ | |
'post_author' => 1 , // 投稿者のID | |
'post_date' => '2012/11/15 20:00:00' , // 投稿時刻 | |
'post_type' => 'posts' , // 投稿タイプ(カスタム投稿タイプも指定できるよ) | |
'post_status' => 'publish' , // publish (公開) とか、draft (下書き) とか | |
'post_title' => 'タイトル' , // 投稿のタイトル | |
'post_content' => '本文' , // 投稿の本文 | |
'post_category'=> array(1, 2) , // カテゴリーID を配列で | |
'post_tags' => array('タグ1', 'タグ2') , // 投稿のタグを配列で | |
)); | |
// メディアを追加 | |
$post->add_media( | |
'/path/to/wordpress/wp-content/uploads/hoge.jpg' , // メディアファイルの絶対パス | |
'タイトル', // メディアの「タイトル」 | |
'説明', // メディアの「説明」 | |
'キャプション', // メディアの「キャプション」 | |
true // true (アイキャッチ画像にする) or false (アイキャッチ画像にしない) | |
); | |
// URL からメディアをダウンロードして追加 | |
// remote_get_file() 関数は、class-wp_post_helper.php に入ってます。 | |
// 引数に URL を渡すと wp_upload_dir にファイルをダウンロードして、絶対パスを返します。 | |
// 失敗した場合は false を返す。 | |
if ( $media = remote_get_file('http://example.com/fuga.jpg') ) { | |
$post->add_media($media, 'タイトル', '説明', 'キャプション', false); | |
} | |
// カスタムフィールドに値を追加 | |
$post->add_meta( | |
'meta_key', // メタキー | |
'meta_val', // メタバリュー | |
true // ユニークにする(true) or しない(false) | |
); | |
// Advanced Custom Field プラグイン形式の値を追加 | |
$post->add_field( | |
'field_xxxxxxxxxxxxx', // キー | |
'field_val' // バリュー | |
); | |
// 投稿 | |
$post->insert(); | |
// 後処理 | |
unset($post); |
細かいことは、後で dogmap.jp に書きます。
コメントを残す