Code Monkey home page Code Monkey logo

our-import-example's Introduction

Our Import

An example command for WP-CLI, to import content from a custom CMS's database. You can just read right through master and follow the comments, or go through it step-by-step through the other branches.

This example was presented at the Boston WP Meetup in May 2013. The slides accompanying it can be found here.

The starter script that this leads to is in the starter-script branch.

Step 1: Files step-one

In this step we've created our-plugin.php & our-cli.php. We put these files in a folder in wp-content/plugins, and activate it in the WP admin. We've also set up the class & named our command ourport.

Step 2: Basic subcommand step-two

We've created the subcommand hello, to show how to use arguments passed in from the command line.

Step 3: Database connection step-three

Since we're importing from a second database, we need to create a new database connection. My preference is to create a setup function that populates a member variable, which is what this step does. We also create a new subcommand test to make sure we've connected.

Step 4: Import a single post step-four

In this step we've created three new functions, but only one of them is a subcommand. Any public method of your class will be a subcommand, you need to specify private if it's just a utility. So we've created the subcommand update which requires an ID, and pulls that one post out of our source database. Then we run it through _import. Read through these functions - the important parts are wp_insert_post, update_post_meta. set_post_format can also be a useful function, and checking out the functions related to wp_insert_post would also be smart.

At this point you can run wp ourport update <id> and you'll see your new post!

Step 4.5: Attach taxonomies step-four-five

Here (starting at line 100), we pull out tags and categories from the source database. Tags can be inserted directly as text, so we don't need to change the $terms result except to manipulate it into the correct format for wp_set_post_terms. Categories need to be the term_id, so we do an array_walk to convert each category to a WordPress category ID (note: this function is not actually written, but you would use term_exists to get the term ID if it exists, and if not you can create it with wp_insert_term). Important functions here are wp_set_post_terms and wp_list_pluck.

Step 5: Import media step-five

By now we've tried the import and can see it imports a post, but maybe we also need the media moved into the media library. To do this, we'll use a regular expression to grab all the <img> tags & pull out the URL. From here we do a little checking to make sure we want to import it, and then using media_sideload_image, we download the image, and move it into the library. This returns HTML of the image tag, so using the original $img we saved, we simply string-replace the old HTML with the new. Now that we've changed the content, we need to update the post with wp_update_post.

The important functions here are media_sideload_image & wp_update_post.

Step 6: Import all posts step-six

Create a new subcommand to grab all items from the database. While there are items, we'll grab an item and run import on it, just like we do in the single post step. You can pass arguments here to skip certain posts (maybe you have a list of IDs already imported).

References

our-import-example's People

Contributors

ryelle avatar

Stargazers

Ryan Veitch avatar Dinesh Kesarwani avatar Pantelis Koukousoulas avatar Christopher Anderton avatar Taylor Dewey avatar Kailey Lampert avatar Grant Mangham avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.