• Home
  • Tutorial
  • AI
  • WordPress
  • UI/UX
  • Web crawling
The TechnoTreat

Type and hit Enter to search

  • Home
  • Tutorial
  • AI
  • WordPress
  • UI/UX
  • Web crawling
TutorialWordpress

Importing bulk content into WordPress – Part 1

Thetechnotreat
May 8, 2019 3 Mins Read
16 Views
2 Comments

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: http://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 id=”28″]

How to import bulk images into WordPress site.

Tags:

ApiPythonRestApiWordpress

Share Article

Follow Me Written By

Thetechnotreat

Other Articles

Bulk image import into wordpress site
Next

Importing bulk content into WordPress – part 2

Next
Bulk image import into wordpress site
May 16, 2019

Importing bulk content into WordPress – part 2

2 Comments

  1. Importing bulk content into Wordpress - part 2 - The TechnoTreat says:
    May 16, 2019 at 9:30 am

    […] the previous tutorial, we learned about how to import content into a WordPress site. In this tutorial, I will explain […]

    Reply
  2. XLS to CSV Converter Software says:
    June 7, 2019 at 11:33 pm

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

    Reply

Leave a Reply Cancel reply

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

All Right Reserved! | Privacy policy