cognito/wpimporter

读取WordPress导出XML文件,并以易于导入到自定义系统或CMS的方式呈现帖子

1.3 2018-07-05 09:46 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:35 UTC


README

此软件包读取WordPress导出文件,并以通用的方式格式化帖子,以便导入到自定义系统或CMS。

安装

使用composer安装非常简单

composer require cognito/wpimporter

过程

在WordPress管理区域,转到工具 > 导出

导出所有内容并下载导出文件。

将文件传输到服务器,并通过调用

<?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();

图片列表

使用postImageList()pageImageList()函数分别提供帖子和相关页面的图片列表。这给出了相对于网站根URL的URL列表。

网站URL存储在$wpimporter->siteurl中,如果自动检测到,则应使用。

所有帖子内容都去除了网站URL,因此链接都是相对于根目录的,而不是包含网站URL。列表中的这些图片是相对链接,从帖子中提取的,因此您需要重新组装以获取完整的URL进行下载。

例如:

<?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,
		));
	}

对于较大的导入,您可能更喜欢将其导出为列表,并使用ajax为用户提供进度。

帖子上的特色图片

要获取特色图片列表,请调用featuredPostImageList()函数。

这返回一个包含帖子ID和图片完整URL的数组。在导入帖子时,您应记录帖子ID,以便将图片下载到系统中并与导入的帖子关联。