Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active March 21, 2023 02:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hissy/0b8df0149fbba16be08d to your computer and use it in GitHub Desktop.
Save hissy/0b8df0149fbba16be08d to your computer and use it in GitHub Desktop.
[Really Simple CSV Importer] 画像をインターネットからダウンロードして投稿に添付し、画像のIDをカスタムフィールドに登録
<?php
/*
Plugin Name: Really Simple CSV Importer Action add-on
Description: Run the additional action after importing the post data
Author: Takuro Hishikawa
Version: 0.1
*/
class rscsvimporter_action {
// singleton instance
private static $instance;
public static function instance() {
if ( isset( self::$instance ) )
return self::$instance;
self::$instance = new rscsvimporter_action;
self::$instance->run_init();
return self::$instance;
}
private function __construct() {
/** Do nothing **/
}
protected function run_init() {
add_action( 'init', array( $this, 'add_action' ) );
}
public function add_action() {
add_filter( 'really_simple_csv_importer_post_saved', array( $this, 'import_media'), 50 );
}
public function import_media($post) {
if (is_object($post)) {
// image というキーのカスタムフィールドのデータを取得
$image = $post->image;
// 投稿IDを指定してRSCSV_Import_Post_Helper のインスタンスを取得
$h = RSCSV_Import_Post_Helper::getByID($post->ID);
// リモートから画像ファイルを取得
$file = $h->remoteGet($image);
// 取得した画像ファイルを投稿に添付
$attachment_id = $h->setAttachment($file);
// image というキーのカスタムフィールドに添付した画像のIDを登録
$h->setMeta( array( 'image' => $attachment_id ) );
}
}
}
$rscsvimporter_action = rscsvimporter_action::instance();
"post_title","post_type","image"
"Import Image to Custom Field test","post","http://example.com/example.jpg"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment