{"id":15692449,"url":"https://github.com/ryelle/our-import-example","last_synced_at":"2025-07-09T19:40:35.630Z","repository":{"id":8695647,"uuid":"10359391","full_name":"ryelle/our-import-example","owner":"ryelle","description":"Example WP-CLI command created to walk through how to import content","archived":false,"fork":false,"pushed_at":"2013-08-19T21:11:09.000Z","size":114,"stargazers_count":7,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-08T02:45:47.978Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryelle.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-29T12:45:00.000Z","updated_at":"2016-03-30T02:10:10.000Z","dependencies_parsed_at":"2022-09-19T06:01:02.080Z","dependency_job_id":null,"html_url":"https://github.com/ryelle/our-import-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ryelle/our-import-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryelle%2Four-import-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryelle%2Four-import-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryelle%2Four-import-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryelle%2Four-import-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryelle","download_url":"https://codeload.github.com/ryelle/our-import-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryelle%2Four-import-example/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264502494,"owners_count":23618619,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T18:33:26.718Z","updated_at":"2025-07-09T19:40:35.529Z","avatar_url":"https://github.com/ryelle.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Our Import\n\nAn 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.\n\nThis example was presented at the Boston WP Meetup in May 2013. [The slides accompanying it can be found here](http://redradar.net/slides/wp-cli/).\n\nThe starter script that this leads to is in the [`starter-script`](https://github.com/ryelle/our-import-example/tree/starter-script) branch.\n\n## Step 1: Files [`step-one`](https://github.com/ryelle/our-import-example/tree/step-one)\nIn this step we've created `our-plugin.php` \u0026 `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 \u0026 named our command `ourport`.\n\n\n## Step 2: Basic subcommand [`step-two`](https://github.com/ryelle/our-import-example/blob/step-two/our-cli.php)\nWe've created the subcommand `hello`, to show how to use arguments passed in from the command line.\n\n\n## Step 3: Database connection [`step-three`](https://github.com/ryelle/our-import-example/blob/step-three/our-cli.php)\nSince 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.\n\n\n## Step 4: Import a single post [`step-four`](https://github.com/ryelle/our-import-example/blob/step-four/our-cli.php)\nIn 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`](http://codex.wordpress.org/Function_Reference/wp_insert_post), [`update_post_meta`](http://codex.wordpress.org/Function_Reference/update_post_meta). [`set_post_format`](http://codex.wordpress.org/Function_Reference/set_post_format) can also be a useful function, and checking out [the functions related to `wp_insert_post`](http://codex.wordpress.org/Function_Reference/wp_insert_post#Related) would also be smart.\n\nAt this point you can run `wp ourport update \u003cid\u003e` and you'll see your new post!\n\n\n## Step 4.5: Attach taxonomies [`step-four-five`](https://github.com/ryelle/our-import-example/blob/step-four-five/our-cli.php)\nHere (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`](http://codex.wordpress.org/Function_Reference/wp_set_post_terms) and [`wp_list_pluck`](http://codex.wordpress.org/Function_Reference/wp_list_pluck).\n\n\n## Step 5: Import media [`step-five`](https://github.com/ryelle/our-import-example/blob/step-five/our-cli.php)\nBy 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 `\u003cimg\u003e` tags \u0026 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`.\n\nThe important functions here are [`media_sideload_image`](http://codex.wordpress.org/Function_Reference/media_sideload_image) \u0026 [`wp_update_post`](http://codex.wordpress.org/Function_Reference/wp_update_post).\n\n\n## Step 6: Import all posts [`step-six`](https://github.com/ryelle/our-import-example/blob/step-six/our-cli.php)\nCreate 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).\n\n\n## References\n- [WP-CLI](http://wp-cli.org/)- see here for install instructions \u0026 basic use.\n- All the codex pages linked\n- [WP-CLI's Commands Cookbook](https://github.com/wp-cli/wp-cli/wiki/Commands-Cookbook)\n- [$wpdb documentation](http://codex.wordpress.org/Class_Reference/wpdb) for reference on the WP database\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryelle%2Four-import-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryelle%2Four-import-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryelle%2Four-import-example/lists"}