If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be
downloaded to a temp file before being sideloaded.
-* `--post-id=<post_id>`
+* `--post_id=<post_id>`
- ID of the post to attach the imported files to.
+ ID of the post to attach the imported files to
+
+* `--title=<title>`
+
+ Attachment title (post title field)
+
+* `--caption=<caption>`
+
+ Caption for attachent (post excerpt field)
+
+* `--alt=<alt_text>`
+
+ Alt text for image (saved as post meta)
* `--desc=<description>`
- "Description" field (post title) of attachment post
+ "Description" field (post content) of attachment post
## EXAMPLES
wp media import ~/Pictures/**/*.jpg
- wp media import ~/Downloads/image.png --post_id=123 --desc="A downloaded picture"
+ wp media import ~/Downloads/image.png --post_id=123 --title="A downloaded picture"
+
+ wp media import http://s.wordpress.org/style/images/wp-header-logo.png --title='The WordPress logo' --alt="Semantic personal publishing"
+
+
.TH "WP\-MEDIA\-IMPORT" "1" "" "WP-CLI"
.
.SH "NAME"
-\fBwp\-media\-import\fR \- Sideload images from file(s) and import as attachments, optionally attached to a post
+\fBwp\-media\-import\fR \- Sideload images from local file(s) or URL and import as attachments, optionally attached to a post
.
.SH "SYNOPSIS"
-wp media import \fIfile\fR\.\.\. [\-\-post_id=\fIpost_id\fR] [\-\-desc=\fIdescription\fR]
+wp media import \fIfile\fR\.\.\. [\-\-post_id=\fIpost_id\fR] [\-\-title=\fItitle\fR] [\-\-caption=\fIcaption\fR] [\-\-alt=\fIalt_text\fR] [\-\-desc=\fIdescription\fR]
.
.SH "OPTIONS"
.
Path to file or files to be imported\. Supports the glob(3) capabilities of the current shell\. If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be downloaded to a temp file before being sideloaded\.
.
.IP "\(bu" 4
-\fB\-\-post\-id=<post_id>\fR
+\fB\-\-post_id=<post_id>\fR
.
.IP
-ID of the post to attach the imported files to\.
+ID of the post to attach the imported files to
+.
+.IP "\(bu" 4
+\fB\-\-title=<title>\fR
+.
+.IP
+Attachment title (post title field)
+.
+.IP "\(bu" 4
+\fB\-\-caption=<caption>\fR
+.
+.IP
+Caption for attachent (post excerpt field)
+.
+.IP "\(bu" 4
+\fB\-\-alt=<alt_text>\fR
+.
+.IP
+Alt text for image (saved as post meta)
.
.IP "\(bu" 4
\fB\-\-desc=<description>\fR
.
.IP
-"Description" field (post title) of attachment post
+"Description" field (post content) of attachment post
.
.IP "" 0
.
wp media import ~/Pictures/**/*\.jpg
-wp media import ~/Downloads/image\.png \-\-post_id=123 \-\-desc="A downloaded picture"
+wp media import ~/Downloads/image\.png \-\-post_id=123 \-\-title="A downloaded picture"
+
+wp media import http://s\.wordpress\.org/style/images/wp\-header\-logo\.png \-\-title=\'The WordPress logo\' \-\-alt="Semantic personal publishing"
.
.fi
/**
* Sideload images from local file(s) or URL and import as attachments, optionally attached to a post
*
- * @synopsis <file>... [--post_id=<post_id>] [--desc=<description>]
+ * @synopsis <file>... [--post_id=<post_id>] [--title=<title>] [--caption=<caption>] [--alt=<alt_text>] [--desc=<description>]
*/
function import( $args, $assoc_args = array() ) {
$assoc_args,
array(
'post_id' => 0,
+ 'title' => null,
+ 'caption' => null,
+ 'alt' => null,
'desc' => null
)
);
'tmp_name' => $file,
'name' => basename( $file )
);
- $success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['desc'] );
+
+ $post_array= array(
+ 'post_title' => $assoc_args['title'],
+ 'post_excerpt' => $assoc_args['caption'],
+ 'post_content' => $assoc_args['desc']
+ );
+
+ $success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['title'], $post_array );
+
+ if ( !is_wp_error( $success ) && $assoc_args['alt'] )
+ update_post_meta( $success, '_wp_attachment_image_alt', $assoc_args['alt'] );
+
if ( is_wp_error( $success ) )
WP_CLI::error(
sprintf(