Migrating Wordpress into Drupal 8

Quite a bit has changed for the Migrate module in Drupal 8: the primary module is part of core and some of the tools have been split into their own modules. Recently, we migrated a Wordpress site into Drupal 8 and this article will help guide you in that process. If you’re looking for information about Wordpress to Drupal 7 migrations, check out Joel Steidl’s article on that here.
At the time of writing this post, the migration modules are considered "experimental" so be aware of that as well. The module's location in core also means that all Drupal core modules also have migration-related code to help out with your Drupal upgrades. We used the WP Migrate module (Migrate Wordpress) as a starting point in bringing this content to Drupal.
This module will give you a good basis for migration, but it is missing a few things that you might want to consider:

  • It will create all vocabularies and taxonomies based on what is in Wordpress but you will need to add some code to connect the taxonomies with posts.
  • Also, it will not bring in featured images.
  • WP content might be using the "line break to paragraphs" functionality, which you need to account for either in your text format for posts or in the migration.

And if you are looking for information about Wordpress to Drupal 7 migrations, check out Joel Steidl's article on that here.

Taxonomy

There's code existing to pull in Wordpress's terms and vocabularies, but you will need to do some work to put them into the right fields with your posts. For this, I ended up taking a more efficient route by querying the source database in prepareRow():

<br />
<?php</p>
<p>// place in Posts.php prepareRow()</p>
<p>// get terms for this blog post<br />
$tags = $this->select('wp_term_relationships', 'r')<br />
  ->join('wp_term_taxonomy', 't', 't.term_taxonomy_id=r.term_taxonomy_id')<br />
  ->fields('r')<br />
  ->condition('t.taxonomy', 'tags')<br />
  ->condition('object_id', $row->getSourceProperty('id'))->execute();<br />
$tags = $tags->fetchAll();<br />
$tags = array_map(function($tag) {<br />
  return intval($tag['term_taxonomy_id']);<br />
}, $tags);<br />
$row->setSourceProperty('tags', $tags);</p>
<p>// get categories for this blog post<br />
$category = $this->select('wp_term_relationships', 'r')<br />
  ->join('wp_term_taxonomy', 't', 't.term_taxonomy_id=r.term_taxonomy_id')<br />
  ->fields('r')<br />
  ->condition('t.taxonomy', 'category')<br />
  ->condition('object_id', $row->getSourceProperty('id'))->execute();<br />
$category = $category->fetchAll();<br />
$category = array_map(function($tag) {<br />
  return intval($tag['term_taxonomy_id']);<br />
}, $category);<br />
$row->setSourceProperty('categories', $category);<br />

And then I updated the migration template with those new values:
<br />
# add to the process section<br />
field_tags: tags<br />
field_category: tags<br />

Featured Images

Wordpress stores featured images as attachment posts and stores the relationship in the postmeta table. To bring these in as image fields, we need to make file entities in Drupal which means configuring a new migration.
First, create a migration template called wp_feature_images.yml. Note that I stole some of this from Drupal's core file module:

<br />
id: wp_feature_images<br />
label: Wordpress Feature Images<br />
migration_tags:<br />
  - Wordpress<br />
migration_group: wordpress<br />
source:<br />
  plugin: feature_images<br />
destination:<br />
  plugin: entity:file<br />
process:<br />
  filename: filename<br />
  uri: uri<br />
  status:<br />
    plugin: default_value<br />
    default_value: 1<br />
# migration_dependencies:<br />
#   required:<br />
#     - wp_users<br />

And then create a source plugin:
<br />
<?php<br />
/<strong><br />
 * @file<br />
 * Contains \Drupal\migrate_wordpress\Plugin\migrate\source\FeatureImages.<br />
 */</p>
<p>namespace Drupal\migrate_wordpress\Plugin\migrate\source;</p>
<p>use Drupal\migrate\Row;<br />
use Drupal\migrate\Plugin\migrate\source\SqlBase;<br />
use Drupal\Core\File\FileSystemInterface;<br />
use Symfony\Component\DependencyInjection\ContainerInterface;<br />
use Drupal\migrate\Plugin\MigrationInterface;<br />
use Drupal\Core\State\StateInterface;</p>
<p>/</strong><br />
 * Extract feature images from Wordpress database.<br />
 *<br />
 * @MigrateSource(<br />
 *   id = "feature_images"<br />
 * )<br />
 */<br />
class FeatureImages extends SqlBase {</p>
<p>  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, FileSystemInterface $file_system) {<br />
    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state);<br />
    $this->fileSystem = $file_system;<br />
  }</p>
<p>  /<strong><br />
   * {@inheritdoc}<br />
   */<br />
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {<br />
    return new static(<br />
      $configuration,<br />
      $plugin_id,<br />
      $plugin_definition,<br />
      $migration,<br />
      $container->get('state'),<br />
      $container->get('file_system')<br />
    );<br />
  }</p>
<p>  /</strong><br />
   * {@inheritdoc}<br />
   */<br />
  public function query() {<br />
    $query = $this<br />
      ->select('wp_postmeta', 'm')<br />
      ->fields('p', ['ID', 'guid']);<br />
    $query->join('wp_posts', 'p', 'p.ID=m.meta_value');<br />
    $query<br />
      ->condition('m.meta_key', '_thumbnail_id', '=')<br />
      ->condition('p.post_type', 'attachment', '=')<br />
      ->condition('p.guid', '', '<>')<br />
      // this prevents some duplicates to get the count closer to even<br />
      ->groupBy('ID, guid');<br />
    return $query;<br />
  }</p>
<p>  /<strong><br />
   * {@inheritdoc}<br />
   */<br />
  public function fields() {<br />
    $fields = array(<br />
      'ID' => $this->t('The file ID.'),<br />
      'guid' => $this->t('The file path'),<br />
    );<br />
    return $fields;<br />
  }</p>
<p>  /</strong><br />
   * {@inheritdoc}<br />
   */<br />
  public function prepareRow(Row $row) {<br />
    $url = $row->getSourceProperty('guid');<br />
    $parsed_url = parse_url($url);<br />
    $filename = basename($parsed_url['path']);<br />
    $row->setSourceProperty('filename', $filename);<br />
    $public_path = 'public://' . $parsed_url['path'];<br />
    $row->setSourceProperty('uri', $public_path);</p>
<p>    // download the file if it does not exist<br />
    if (!file_exists($public_path)) {<br />
      $public_dirname = dirname($public_path);</p>
<p>      // create directories if necessary<br />
      if (!file_exists($public_dirname)) {<br />
        $this->fileSystem->mkdir($public_dirname, 0775, TRUE);<br />
      }</p>
<p>      // try to download it<br />
      $copied = @copy($url, $public_path);<br />
      if (!$copied) {<br />
        return FALSE;<br />
      }<br />
    }<br />
    return parent::prepareRow($row);<br />
  }</p>
<p>  /<strong><br />
   * {@inheritdoc}<br />
   */<br />
  public function bundleMigrationRequired() {<br />
    return FALSE;<br />
  }</p>
<p>  /</strong><br />
   * {@inheritdoc}<br />
   */<br />
  public function getIds() {<br />
    return array(<br />
      'ID' => array(<br />
        'type' => 'integer',<br />
        'alias' => 'p',<br />
      ),<br />
    );<br />
  }</p>
<p>}<br />

In Migrate, the template defines what source, processing, and fields are created. The source plugin is used by that migration to allow you to specify what is created. The source plugin above will get the feature images for posts, but also try and download the image into Drupal's files directory.
You can add this as a dependency for the wp_posts migration. A word of warning though: if one migration (Migration A) depends on a different migration (Migration B), all of the content from A must be migrated before B can be run. If there are images that cannot be resolved for some reason (maybe leftover DB references after an image or post is deleted), this might stop the migration because the dependency cannot be resolved.
And finally, you will also need to add "wp_feature_images" to your manifest_wordpress.yml before running the migration.

Converting content

So far we have updated migration source plugins, but there are also process plugins, which can be used to change row values. As mentioned, the WP content often uses the autop filter to create paragraph/line breaks automatically so we need to change those to HTML for Drupal. (You can also just use this functionality in your text format and skip this step if having this on will not cause issues with other content)
First, create a "src/Plugin/migrate/process" directory if one does not exist in the module and add this processor:

<br />
<?php</p>
<p>namespace Drupal\migrate_wordpress\Plugin\migrate\process;</p>
<p>use Drupal\migrate\MigrateExecutableInterface;<br />
use Drupal\migrate\ProcessPluginBase;<br />
use Drupal\migrate\Row;</p>
<p>/<strong><br />
 * Apply the automatic paragraph filter to content<br />
 *<br />
 * @MigrateProcessPlugin(<br />
 *   id = "wp_content"<br />
 * )<br />
 */<br />
class WpContent extends ProcessPluginBase {</p>
<p>  /</strong><br />
   * {@inheritdoc}<br />
   *<br />
   * Split the 'administer nodes' permission from 'access content overview'.<br />
   */<br />
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {<br />
    return _filter_autop($value);<br />
  }</p>
<p>}<br />

Then, update the "process" section of "wp_posts.yml" to include this processor:
<br />
'body/value':<br />
    plugin: wp_content<br />
    source: post_content<br />

All of this should put you on the road to getting Wordpress content migrated into a Drupal 8 site, although you’ll probably have to adjust code to your specific circumstances along the way.

Code Drupal Drupal 8 Drupal Planet

Read This Next