Skip to content
The TechnoTreat
  • Home
  • Contact

Importing bulk content into WordPress – part 2

By Thetechnotreat

May 16, 2019in Tutorial, Wordpress 1 comment

  • Python
  • RestApi
  • Wordpress
Spread the love

On the previous tutorial, we learned about how to import content into a WordPress site. In this tutorial, I will explain how to import bulk images into a WordPress site.

I will also put a download link of a script with sample files so you can install and run it on your system.

Please go through part 1 on things that are required before using the API feature of WordPress.

Here’s the quick overview of requirements before we start.

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

Libraries used for uploading images are,

import requests
import csv
import json
import os
import re
def upload_images(self):
    """upload files from local directory to the wordpress site

    :returns: ids of uploaded images | list

    """
    url = self.domain+"/wp-json/wp/v2/media/"
    upload_file_ids = []
    with os.scandir('images/') as d:
        for entry in d:
            if entry.is_file():
                path = entry.path
                filename = re.search('.*\/(.*?)\.(.*)', path)
                if filename:
                    name = filename.group(1)
                    image_type = filename.group(2)
                else:
                    return None
                data = open(path, 'rb').read()
                headers = dict()
                headers['Content-Type'] = 'image/'+image_type
                headers['Content-Disposition'] = 'attachment; filename=%s'% name+"."+image_type
                response = self.request.post(url, data=data, headers=headers)
                content = json.loads(response.text)
                upload_file_ids.append(content['id'])
    return upload_file_ids

This function loops through all the files in the images folder and uploads them into WordPress.

Out put after running image import script
Media library after importing images

Download complete code with a sample image.

Download “Full script with post and image import” blog_wordpress_import-1.zip – Downloaded 48 times – 84 KB

Import bulk content into WordPress site with sample data and running python script.

  • Previous Post
    Importing bulk content into Wordpress - Part 1
  • Next Post
    James Donkey 008 Tactical Master Gaming - Headphone For PUBG Mobile

One thought on “Importing bulk content into WordPress – part 2”

  • Pingback: Importing bulk content into Wordpress - Part 1 - The TechnoTreat

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