LoginSignup
0
0

More than 5 years have passed since last update.

WordPress メディアファイル一括登録

Last updated at Posted at 2019-04-25

WP-CLI で提供されていても良いと思いますが、(多分)ないのでスクリプトを用意しました。

<?php
require_once __DIR__ . '/../../../wp-load.php';
require_once __DIR__ . '/../../../wp-admin/includes/media.php';
require_once __DIR__ . '/../../../wp-admin/includes/image.php';
require_once __DIR__ . '/../../../wp-admin/includes/file.php';

$source_directory      = $argv[1];
$destination_directory = $argv[2];

foreach (glob($source_directory . '*') as $file_name) {
    if ( ! is_file($file_name)) {
        continue;
    }

    $destination_file_name = $destination_directory . basename($file_name);
    rename($file_name, $destination_file_name);

    $file_type_map = wp_check_filetype(basename($destination_file_name), null);
    $attachment    = [
        'guid'           => $destination_file_name,
        'post_mime_type' => $file_type_map['type'],
        'post_title'     => preg_replace('/\.[^.]+$/', '', basename($destination_file_name)),
        'post_content'   => '',
        'post_status'    => 'inherit'
    ];
    $attachment_id = wp_insert_attachment($attachment, $destination_file_name, 0);
    wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $destination_file_name));

    printf("%d : %s\n", $attachment_id, $destination_file_name);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0