Last active
March 21, 2023 02:15
-
-
Save hissy/0b8df0149fbba16be08d to your computer and use it in GitHub Desktop.
[Really Simple CSV Importer] 画像をインターネットからダウンロードして投稿に添付し、画像のIDをカスタムフィールドに登録
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: 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(); |
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
"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