2017 © Pedro Peláez
 

library wpimporter

Reads Wordpress export XML file and presents posts in a way that is easy to import to a custom system

image

cognito/wpimporter

Reads Wordpress export XML file and presents posts in a way that is easy to import to a custom system

  • Thursday, July 5, 2018
  • by joshbmarshall
  • Repository
  • 1 Watchers
  • 0 Stars
  • 115 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 47 % Grown

The README.md

WPImporter

This package reads a wordpress export file and formats the posts in a generic way that can be imported into a custom system or CMS., (*1)

Installation

Installation is very easy with composer:, (*2)

composer require cognito/wpimporter

Process

In the Wordpress administration area, go to Tools > Export, (*3)

Export all content and download the export file., (*4)

Transfer the file to the server and parse it by calling, (*5)

<?php
    $filename = '/the/path/to/the/file.xml';
    $siteurl = 'https://www.sitename.com'; // optional, if left blank it will auto-detect from the xml file (recommended)
    $wpimporter = new \Cognito\WPImporter\WPImporter($filename, $siteurl);

    // Get the posts
    while ($post = $wpimporter->getPost()) {
        var_dump($post);
    }

    // Get the pages
    while ($page = $wpimporter->getPage()) {
        var_dump($page);
    }

    // Get a custom post type, such as faq
    while ($faq = $wpimporter->getPostType('faq')) {
        var_dump($faq);
    }

    // Get the list of images in posts
    $post_img_urls = $wpimporter->postImageList();

    // Get the list of images in pages
    $page_img_urls = $wpimporter->pageImageList();

Image list

The list of images for posts and pages are provided using the postImageList() and pageImageList() functions respectively. This gives a list of urls relative to the root of the siteurl., (*6)

The site url is stored in $wpimporter->siteurl so if autodetected this should be used., (*7)

All post content has the siteurl stripped so the links are all relative to root rather than including the site url. These images in the list are the relative links as pulled from the posts, so you will have to re-assemble to get the full url to download., (*8)

An example would be:, (*9)

<?php
    $basepath = '/path/to/wwwroot';

    foreach ($post_img_urls as $img_url) {
        // Ensure folder exists
        $folder = $basepath . dirname($img_url);
        if (!file_exists($folder)) {
            mkdir($folder, 0750, true);
        }

        $filename = $folder . '/' . basename($img_url);
        if (file_exists($filename) && filesize($filename) > 0) {
            // Do not overwrite
            continue;
        }

        // Download the file
        $src = $wpimporter->siteurl . $img_url;

        // e.g. using GuzzleHttp
        $client = new \GuzzleHttp\Client();
        $client->request('GET', $src, array(
            'sink' => $filename,
        ));
    }

For larger imports you may prefer to export this as a list and use ajax to give progress to the user., (*10)

To get a list of featured images, call the featuredPostImageList() function., (*11)

This returns an array containing the post id and the full url to the image. You should record the post id when importing the posts so that you can download the image into the system and relate it to the imported post., (*12)

The Versions

05/07 2018

dev-master

9999999-dev

Reads Wordpress export XML file and presents posts in a way that is easy to import to a custom system

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

by Josh Marshall

05/07 2018

1.3

1.3.0.0

Reads Wordpress export XML file and presents posts in a way that is easy to import to a custom system

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

by Josh Marshall

07/03 2018

1.2

1.2.0.0

Reads Wordpress export XML file and presents posts in a way that is easy to import to a custom system

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

by Josh Marshall

04/03 2018

1.1

1.1.0.0

Reads Wordpress export XML file and presents posts in a way that is easy to import to a custom system

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

by Josh Marshall

26/02 2018

1.0

1.0.0.0

Reads Wordpress export XML file and presents posts in a way that is easy to import to a custom system

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

by Josh Marshall