This project provides a Python script to import Markdown blog posts into a WordPress site using the WordPress REST API. The script reads metadata and content from Markdown files, converts the content to HTML, and creates corresponding posts on WordPress.
- Extracts metadata such as title, date, tags, categories, and language from Markdown files.
- Converts Markdown content to HTML.
- Supports slug customization by extracting the last segment of the URL.
- Automatically posts content to WordPress with proper authentication.
- Supports different Markdown parsers: normal, Hugo, and Jekyll.
- Extracts tags and categories from Hugo build HTML files.
- Imports from Jekyll and Hugo single files.
- Crawls Hugo build directories to extract and import posts.
- Exports posts to WordPress eXtended RSS (WXR) format.
-
Python 3.x installed on your system.
-
WordPress REST API enabled on your WordPress site.
-
A WordPress account with sufficient permissions to create posts.
-
Required Python libraries installed:
pip install markdown PyYAML requests python-dotenv beautifulsoup4
-
Clone or download this repository.
-
Navigate to the project directory:
cd markdown-to-wordpress
-
Copy
.env.exampleto.envand fill in your credentials:cp .env.example .env
-
Edit
.envto include your WordPress details:WORDPRESS_URL=https://yourwordpresssite.com/wp-json/wp/v2 USERNAME=your_username PASSWORD=your_password MARKDOWN_DIRECTORY=/path/to/markdown/files MARKDOWN_PARSER=normal # Options: normal, hugo, jekyll, hugo_build EXPORT_MODE=import # Options: import, export EXPORT_DIRECTORY=/path/to/export/directory # Required if EXPORT_MODE is 'export' -
Install dependencies:
pip install -r requirements.txt
-
Ensure all your Markdown files are in the specified directory. Each file should follow the format:
- YAML front matter for metadata (e.g., title, date, tags, categories, etc.).
- Markdown content below the front matter.
-
Run the script:
python import_to_wordpress.py
-
The script will:
- Read each
.mdfile in the directory. - Parse the YAML front matter and Markdown content.
- Convert the content to HTML using the specified parser.
- Create a new WordPress post using the WordPress REST API.
- If using Hugo build, it will crawl the build directory to extract and import posts.
- If EXPORT_MODE is set to 'export', it will export posts to WXR format instead of posting to WordPress.
- Read each
---
title: "The Hidden World of Esoteric Programming Languages"
date: 2024-08-13T16:42:19+03:30
url: 2024/08/13/the-hidden-world-of-esoteric-programming-languages/
shortlink: https://g.omid.dev/hQlrMEU
tags:
- Esoteric Programming Languages
- Brainfuck
- Whitespace
lang: en
categories:
- TechBlog
---
Your Markdown content goes here.- To test the script without publishing posts, change
"status": "publish"to"status": "draft"in thepost_to_wordpressfunction. - Ensure your WordPress site allows remote API access.
- Use the "Application Passwords" plugin for secure authentication.
- Authentication Error: Ensure the username and password are correct and the API endpoint is accessible.
- Invalid API Response: Check your WordPress logs for details or verify your metadata format in the Markdown files.
- Markdown Conversion Issues: Ensure the Markdown follows proper syntax, as the script uses the
markdownlibrary for conversion.
This project is licensed under the MIT License. Feel free to use and modify it as needed.