Skip to content
The TechnoTreat
  • Home
  • Contact

Importing bulk content into WordPress – Part 1

By Thetechnotreat

May 8, 2019in Tutorial, Wordpress 2 comments

  • Api
  • Python
  • RestApi
  • Wordpress
Spread the love

WordPress is shipped with rest API which makes migrating or importing contents into WordPress sites very easy. If you have bulk contents for example 100s of posts. It’s not possible to create post manually however you can use rest API features to automate the task.

There are few things required before importing contents,

  1. Basic – Auth Plugin: https://github.com/WP-API/Basic-Auth
  2. Python >= 3
  3. Administrator username and password of your WordPress site.

Basic-Auth plugin is very useful while using WordPress’ rest API. Unfortunately, it is not hosted on the WordPress site. So you can not search and install from the WordPress admin dashboard. Therefore you have to manually download and install it. Here’s the instruction on how to install https://github.com/WP-API/Basic-Auth

json basic auth after installation

After you have successfully installed you are ready to use the admin username and password for rest API.

You also need to change permalink settings. Under common settings choose any type other than ‘Plain’. WordPress has ‘Plain’ selected by default. Change it to anything except ‘Plain’.

Permalink settings for rest api

I am using python3.6 to handle the API request. You can use any other programming language or even simple Curl request can do the job if you know how to use it.

I have created a python class and setup localhost at http://localhost:8000

Python library used here are requests, CSV and JSON. You only need to install requests (https://pypi.org/project/requests/). Others are available by default in python3 and above.

import requests
import csv
import json

After importing python libraries here’s the code for initializing class.

class WordPress(object):

    """Docstring for MyClass. """

    def __init__(self, domain, username, password):
        """
        :username: wordpress username
        :password: wordpress password
        :domain: wordpress domain name | eg: https://thetechnotreat.com

        """
        self.domain = domain
        self._username = username
        self._password = password
        self.request = requests.session()
        self.request.auth = (self._username, self._password)

Note that user must have administrator privilege or it won’t be able to create posts.

Function to create post

    def upload_post(self, title, body, category):
        """create new post

        :title: post title
        :body: post body
        :category: category ids
        :returns: created post detail (dict)

        """
        info = dict()
        info['title'] = title
        info['content'] = body
        info['categories'] = ",".join(category)
        response = self.request.post(self.domain+'/wp-json/wp/v2/posts', json=info)
        return_text = json.loads(response.text)
        return return_text

upload_post function takes three arguments. ‘title’ is the title of the post, ‘body’ is the main content of the post and ‘category’ is the category of the post. While creating a post with its category, rest API takes category id rather than category name. So there must be a category already created on site before creating a post.

Here is the function to check and create category on site.

    def create_category(self, category):
        """create new category if not available
        :returns: category id

        """
        url = self.domain+"/wp-json/wp/v2/categories"
        response = self.request.get(url)
        categories = json.loads(response.text)
        for cat in categories:
            if cat['name'] == category:
                return cat['id']
        data = dict()
        data['name'] = category
        response = self.request.post(url, json=data)
        upload_info = json.loads(response.text)
        if 'id' in upload_info:
            return upload_info['id']
        else:
            return None

Now you just need to loop through CVS files where your collected posts are.

with open('sample_posts.csv') as csv_file:
    reader = csv.DictReader(csv_file)
    wp = WordPress('http://localhost:8000', 'technotreat', 'lVBO4lTmsCMd3ImQ^q')
    count = 1
    for post in reader:
        categories = post['category'].split(",")
        cat_ids = []
        for c in categories:
            id = wp.create_category(c)
            if id:
                cat_ids.append(str(id))

        upload_text = wp.upload_post(post['title'], post['content'], cat_ids)
        print (str(count) + " post imported")
        count += 1
After importing post using rest api

Download “Full script with sample data” blog_wordpress_import.zip – Downloaded 49 times – 2 KB

How to import bulk images into WordPress site.

  • Previous Post
    Hello World!
  • Next Post
    Importing bulk content into Wordpress - part 2

2 thoughts on “Importing bulk content into WordPress – Part 1”

  • Pingback: Importing bulk content into Wordpress - part 2 - The TechnoTreat
  • XLS to CSV Converter Software says:
    June 7, 2019 at 11:33 pm

    Some genuinely interesting info , well written and loosely user pleasant.

    Reply

Reply or Comment Cancel reply

Your email address will not be published. Required fields are marked *

*
*

Search

Follow me

Recent Posts

  • Capturing background requests with Puppeteer
  • Web scraping with Python 3, Requests and Beautifulsoup (bs4)
  • James Donkey 008 Tactical Master Gaming – Headphone For PUBG Mobile
  • Importing bulk content into WordPress – part 2
  • Importing bulk content into WordPress – Part 1

Archives

  • November 2020
  • July 2019
  • June 2019
  • May 2019
© Copyright thetechnotreat.com 2019