Skip to content

bu-ist/wp-cli-import-csv

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 

Repository files navigation

WP CLI Import CSV

importcsv is a WP CLI command for flexible importing of content from .csv formatted files to WordPress.

Back up your database.

importcsv Subcommands

There are 2 subcommands included to check the validity of your arguments, and file header settings. write is the only subcommand which will write to the database. The terminal output allows you to review data mapping what will be imported before asking for confirmation of database write.

  • check - validates import file headers
  • map - maps data using headers ( uses check )
  • write - import data to database ( uses check and map )

Command Arguments & Syntax

The syntax and arguments are the same for all the subcommands.

Here is an example which would check file headers, then mapping of a file named engineers.csv ( in the current directory ) to be imported to the pu01_engineers post type, and set jennyengineer as the author.

When setting up headers the first time, use the check command to only display initial file, post type, author and header settngs:

wp importcsv check <filename>.csv --post_type=<post type name> --author=<username or id>

Once headers validate, check the data mapping:

wp importcsv map <filename>.csv --post_type=<post type name> --author=<username or id>

If no error messages display, write to the database:

wp importcsv write <filename>.csv --post_type=<post type name> --author=<username or id>

post type can be over-ridden using a correctly named header field

Optional Command Parameters

Specify a post type

--post_type=<post type name>

Specify an author for posts

--author=<username or id>

For importing and attaching of thumbnails ( multiple if desired ) to posts, either ensure the path in the database field is correct, or you can add a ( --thumbnail_path=url or relative path ) argument to prepend file names in a thumbnail field while sideloading into the media library.

--thumbnail_path=<url or relative path>

CSV Import File Header Formatting

Before you import, you'll need to format the header of your .csv file.

The importer uses the headers ( first line ) of files with specific formatting to determine how each column will be imported.

There are 3 parameters to set in each column header, separated by a hyphen ( - ):

  • Data Type (post|meta|taxonomy|thumbnail)
  • Sanitization/Escaping callback function (function must be defined, or use blank to skip)
  • Field name (underscored delimited field name)

*** If a column is blank for some reason, simply name it's header 'blank'

Data Types

Post

The post data type may only contain valid post fields. The data is added using wp_insert_post()

Example of a header for a field that would contain the post title:

post-wp_kses_post-post_title

If using a previously created post, just specify the post ID:

post-blank-ID

Meta

Meta data is attached to the inserted post using add_post_meta(). This example will attach the column of data to post meta data with the key _home_town:

meta-sanitize_key-_home_town

Taxonomy

Attaches posts to any registered taxonomy using wp_set_object_terms(). This sets namespaced custom taxonomy, 'kbof_team':

taxonomy-sanitize_title_with_dashes-kbof_team

Only one term may be set at this time.

Thumbnails

Thumbnails may be attached to posts, sideloaded from external url's or relative paths, depending on your needs and the values in your import file.

With this example, we use the specific name 'featured_image' to specify these images are to be attached as the default post thumbnail.

thumbnail-absint-featured_image

For users of plugins like Multiple Post Thumbnails, other names will be set in post meta.

If we use the header parameters importing to a post type with the name pu01_engineers:

thumbnail-absint-secondary_image

The meta key will be the following:

pu01_engineers_secondary_image_thumbnail_id

Data Validation

The second parameter in each column header specifies the sanitization/validation callback used when saving data. The script will check that the functions are defined, but ultimately sanitization is up to you. This feature also gives you the ability to implement validation by creating your own sanitization functions.

Field Names

The third and last parameter is field name. It is either a valid post field, meta key of your choice, namespace of your thumbnail image, or a taxonomy

Useful WordPress Codex Pages

More WP CLI

For more information about the ultra powerful WP CLI, visit the website - http://wp-cli.org, or browse the tools here on Github - https://github.com/wp-cli.

Changelog

0.0.1-beta2

  • Added Changelog
  • Added actions on each import item
  • Check for WP-CLI dependency before running
  • Force php auto line break detection for csvs
  • Updated @synopsis syntax to be compatible with latest WP-CLI
  • Updated author id check to properly accept integer values across all machines

About

WP CLI Command for flexible importing of content from .csv formatted files to WordPress.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%