Tedasuke Image Fields for Really Simple CSV Importer

By tedasuke

0+ active installationsVersion 1.0.0

Add-on for Really Simple CSV Importer. Adds images from CSV URLs to the Media Library and stores their IDs in custom fields and ACF image fields.

Description

This plugin is an add-on for Really Simple CSV Importer (version 1.3.1 or later). While you import posts from a CSV file, it downloads the images whose URLs are written in the CSV, adds them to the Media Library, and stores the ID of each image in its custom field.

It only uses the filters and the action that Really Simple CSV Importer provides and does not extend the importer’s classes. It was tested with WordPress 6.5, 7.0 and 7.1.

Which columns are handled

  • Image fields of Advanced Custom Fields (or Secure Custom Fields) that sit at the top level of a field group. No settings are needed. The column can be named after the field name or the field key. In both cases get_field() returns the value in the field’s return format.
  • Any other custom field column that you list under Tools > CSV Image Fields.

Other columns are left alone, even when a cell holds the URL of an image.

What happens to a cell

  • A cell with one URL that starts with http:// or https://: the image is downloaded and registered, and the field receives its ID. The new media item is attached to the imported post.
  • A URL that appears several times in one import is downloaded and registered once. Every cell with that URL receives the same ID, and the media item stays attached to the first post that used it. A later import registers the same URL again.
  • A value that does not start with http:// or https:// goes to Really Simple CSV Importer as it is: the ID of a media item, a file name, a path on the server, a URL that starts with something else such as ftp://, or an empty cell.
  • If the image cannot be registered, nothing is written to that field. A new post gets no value, and a post that is being updated keeps the value it had. The import goes on with the next row.
  • After the import, the screen shows how many images were added and how many failed.

Safety

  • Only http and https URLs are fetched, through the WordPress HTTP API. Addresses inside the server’s own network, ports other than 80, 443 and 8080, and URLs with a user name are refused, also when a URL redirects to one. The address of the site itself can always be fetched.
  • Only JPEG, PNG, GIF and WebP images are registered. The type is decided from the content of the file, not from its name, and the file is stored with the extension that matches its content.
  • A download may take up to 30 seconds and may be up to 20 MB.
  • If a server does not answer five downloads in a row, the rest of that import does not ask that server again. Those URLs count as failed.
  • An image that is small as a file but so large in width and height that resizing it would use up PHP’s memory is not registered and counts as failed. This check is only made when WordPress resizes images with the GD library. With Imagick it is not made.
  • If a cell holds a path on the server, the file at that path is never read.

The plugin does nothing on the pages your visitors see, adds no script or stylesheet, and contacts no server other than the ones in the URLs of your CSV.

If you used RS CSV Importer Media Add-On

RS CSV Importer Media Add-On did a similar job. Its latest version, 1.1.0, was released in 2015. On WordPress 7.1 and later (tested with 7.1.5), a CSV import stops at the first row with a fatal error while that add-on is active. This plugin can take over the image URLs. It is not a copy of that add-on, and it behaves differently in these points:

  • Only the columns described above are handled, not every cell that looks like the URL of a file.
  • Only images are registered, not documents or other files.
  • A path on the server is not registered. The value goes to Really Simple CSV Importer as it is.

As with the old add-on, the same URL is registered once per import, and again in the next import.

While the old add-on is active, this plugin shows a notice that asks you to deactivate it. It does not deactivate it for you.

This plugin is not affiliated with or endorsed by the authors of Really Simple CSV Importer, RS CSV Importer Media Add-On, Advanced Custom Fields or Secure Custom Fields.

What this plugin does not do

  • It does not touch the post_thumbnail column. Really Simple CSV Importer sets the featured image from that column by itself, as it does without this plugin, and registers the image again for every post, also when the URL is the same.
  • It does not reuse an image from an earlier import. The same URL is registered once in every import.
  • A field holds one image. A cell with several URLs is not registered and counts as failed. Nothing is written to the field.
  • PDF and other files are not registered. File fields and gallery fields of Advanced Custom Fields are not handled.
  • Fields inside a group, repeater or flexible content field of Advanced Custom Fields are not picked up automatically.
  • It does not look up media by file name.
  • It does not record which rows failed or why. You only get the two totals.
  • Images inside the post content are not handled.

What has not been tested

  • Shared hosting. The plugin was only tested on local test sites (WordPress Playground, and WordPress with MariaDB).
  • Advanced Custom Fields PRO, multisite and MySQL.
  • Large CSV files. The largest test had 30 rows.
  • Versions of Really Simple CSV Importer other than 1.3.1.
  • What happens to an image with a very large width and height on a server that uses Imagick.

Tedasuke CSV Media Import (paid)

The author also sells a separate plugin, Tedasuke CSV Media Import. It does not register a URL again in later imports. It registers the image in the post_thumbnail column only once, even when many posts use the same URL. It also accepts several images in one field, handles PDF and file fields, takes media given by file name or ID, and keeps a log of every row with the reason for each failure. It is sold on note.com, and its page is written in Japanese: https://note.com/tedasuke_dev/n/neff32e3c4863

Nothing in this free add-on is locked or limited in time. While Tedasuke CSV Media Import is active, this add-on does nothing and says so on the Plugins screen.

日本語での説明

Really Simple CSV Importer(1.3.1以降)に足して使うアドオンです。CSV から記事をインポートするときに、CSV に書いた URL の画像を取得して「メディア」に登録し、その ID をカスタムフィールドに入れます。

Really Simple CSV Importer が用意しているフィルターとアクションを使っているだけで、Really Simple CSV Importer のクラスは継承していません。WordPress 6.5、7.0、7.1 で動作を確かめました。

対象になる列

  • Advanced Custom Fields(または Secure Custom Fields)の画像フィールドのうち、フィールドグループの直下にあるもの。設定は要りません。列の名前はフィールド名でもフィールドキーでもよく、どちらの場合も get_field() は「戻り値の形式」どおりの値を返します。
  • そのほかのカスタムフィールドの列は、「ツール」→「CSV Image Fields」の画面に名前を書いたものだけが対象です。

これ以外の列には、画像の URL が書いてあっても何もしません。

セルの扱い

  • http:// か https:// で始まる URL が1つ書いてあるセルは、画像を取得して登録し、フィールドにその ID を入れます。登録したメディアは、インポートした記事に添付されます。
  • 1回のインポートの中に同じ URL が何度出てきても、取得と登録は1回だけです。どのセルにも同じ ID が入り、メディアは最初に使った記事に添付されたままになります。次のインポートでは、同じ URL でもまた登録します。
  • http:// や https:// で始まらない値(メディアの ID、ファイル名、サーバー上のファイルの場所、ftp:// などで始まるもの、空のセル)は、そのまま Really Simple CSV Importer に渡します。
  • 画像を登録できなかったときは、そのフィールドに何も書き込みません。新しい記事ではフィールドが無いままになり、登録済みの記事を更新する行では元の値が残ります。インポートは次の行へ進みます。
  • インポートが終わると、登録した件数と失敗した件数が画面に出ます。

安全のための確認

  • 取得するのは http と https の URL だけで、WordPress の HTTP API を通します。サーバーと同じネットワークの中のアドレス、80・443・8080 以外のポート番号、ユーザー名つきの URL は取得しません。転送先がそうだった場合も同じです。インポートするサイト自身の URL は取得できます。
  • JPEG、PNG、GIF、WebP の画像だけを登録します。種類はファイルの名前ではなく中身で判断し、中身に合った拡張子で保存します。
  • 取得は1件につき30秒まで、大きさは20MB までです。
  • 同じサーバーが5回続けて応答しなかったときは、そのインポートの残りでは、そのサーバーに取りに行きません。その分は失敗に数えます。
  • ファイルサイズは小さいのに縦横のピクセル数が大きすぎて、縮小版を作るときに PHP のメモリーが足りなくなる画像は、登録しません(失敗に数えます)。この確認をするのは、WordPress が縮小版を作るのに GD ライブラリを使う環境だけです。Imagick を使う環境では確認しません。
  • セルにサーバー上のファイルの場所(パス)が書いてあっても、そのファイルは読みません。

訪問者が見るページでは何もしません。スクリプトもスタイルシートも足さず、CSV に書いた URL の取得のほかに外部との通信はありません。

RS CSV Importer Media Add-On を使っていた場合

RS CSV Importer Media Add-On は似た役目のアドオンで、最新版の1.1.0 は2015年に出たものです。WordPress 7.1以降では(確かめたのは7.1.5です)、有効にしてあると CSV のインポートが1行目で致命的エラーになって止まります。画面には「重大なエラー」と出ます。画像の URL については、このプラグインが代わりになります。作りは別物で、動きには次の違いがあります。

  • 対象は上に書いた列だけです。ファイルの URL らしく見えるセルをすべて登録することはしません。
  • 画像だけを登録し、文書などのファイルは登録しません。
  • サーバー上のファイルの場所(パス)を書いても登録しません。値はそのまま Really Simple CSV Importer に渡ります。

同じ URL の扱いは旧アドオンと同じで、1回のインポートの中では1回だけ登録し、別のインポートではまた登録します。

旧アドオンが有効になっていると、無効化を促す案内を出します。自動で無効化はしません。

このプラグインは、Really Simple CSV Importer、RS CSV Importer Media Add-On、Advanced Custom Fields、Secure Custom Fields の作者とは関係がなく、公認も受けていません。

できないこと

  • アイキャッチ画像の列(post_thumbnail)には関わりません。アイキャッチ画像は、このプラグインが無いときと同じように Really Simple CSV Importer が設定します。同じ URL でも、記事ごとに登録されます。
  • 前のインポートで登録した画像は使い回しません。同じ URL でも、インポートのたびに1回登録します。
  • 1つのフィールドに入る画像は1枚です。URL を複数書いたセルは登録せず、失敗に数えます。フィールドには何も書き込みません。
  • PDF などのファイルは登録しません。Advanced Custom Fields のファイルフィールドとギャラリーフィールドは対象外です。
  • Advanced Custom Fields の「グループ」「繰り返し」「柔軟なコンテンツ」の中にあるフィールドは、自動では対象になりません。
  • ファイル名からメディアを探すことはしません。
  • どの行が、なぜ失敗したかは記録しません。登録と失敗の件数しか分かりません。
  • 本文の中の画像は扱いません。

確かめていないこと

  • レンタルサーバーでの動作。手元の試験環境(WordPress Playground と、MariaDB を使う WordPress)でしか確かめていません。
  • Advanced Custom Fields PRO、マルチサイト、MySQL。
  • 行数の多い CSV。試験では30行まで流しました。
  • Really Simple CSV Importer 1.3.1 以外のバージョン。
  • 画像の処理に Imagick を使うサーバーで、縦横の大きすぎる画像がどうなるか。

有料の Tedasuke CSV Media Import について

別のプラグイン Tedasuke CSV Media Import を、note で販売しています。そちらは、あとのインポートで同じ URL を登録し直しません。post_thumbnail の列に書いた画像も、同じ URL なら何件の記事で使っても1回だけ登録します。ほかに、1つのフィールドに複数の画像を入れること、PDF とファイルフィールドの扱い、ファイル名や ID での指定、失敗の理由つきの行ごとの記録ができます。販売ページ: https://note.com/tedasuke_dev/n/neff32e3c4863

この無料のアドオンに入っている機能は、有料のプラグインを買わなくても、すべて期限なしで使えます。Tedasuke CSV Media Import が有効なあいだ、このアドオンは何もせず、「プラグイン」画面にそのことを表示します。

Installation

  1. Install and activate Really Simple CSV Importer (1.3.1 or later).
  2. Install and activate this plugin.
  3. If your image columns are not image fields of Advanced Custom Fields, list their names under Tools > CSV Image Fields.
  4. Import your CSV from Tools > Import > CSV as usual.

FAQ

What do I write in the CSV?

The full URL of the image, starting with http:// or https://, one URL per cell. File names with Japanese characters or spaces can be written as they are.

Do I have to list my columns?

Not for image fields of Advanced Custom Fields or Secure Custom Fields at the top level of a field group. The plugin’s screen under Tools > CSV Image Fields shows the fields it found. Every other column has to be listed there, one column name per line, exactly as in the first line of your CSV.

Does it work without Advanced Custom Fields?

Yes. List the columns under Tools > CSV Image Fields. The custom field then holds the ID of the media item.

An image failed. How do I find it?

The field of that post is empty, or still holds its old value if the row updated an existing post. This plugin does not record which row failed or why. Common reasons are a URL that returns an error such as 404, a file that is not a JPEG, PNG, GIF or WebP image, a file larger than 20 MB, a server that does not answer within 30 seconds, a URL that points to an address inside your own network, and a cell with more than one URL.

The same image is in the Media Library several times. Why?

Inside one import, a URL is registered once, however many rows use it. The plugin does not remember URLs between imports, so importing the same URL again on another day adds another copy.

What about the featured image?

Really Simple CSV Importer sets it from the post_thumbnail column, with or without this plugin. This plugin does not touch that column.

Can I change the size limit or the time limit?

Developers can use the filters tdkma_max_file_size (bytes) and tdkma_download_timeout (seconds).

What is removed when I delete the plugin?

The plugin’s setting, which is the list of columns. The media items it registered and the IDs it wrote to your posts stay.

Does it send anything to an outside server?

No. The only requests it makes are the downloads of the URLs written in your CSV.

Screenshots

Changelog

1.0.0

  • First release.