Who am I?
Web developer
Work at Flint Interactive in Collingwood
http://www.flintinteractive.com.auWorking on and off with WordPress since v1.2
So 9 years.
Find me on Twitter: @navitronic
Introduction
- We all know what WordPress is?
- In most minds, a blogging engine.
- Also, when coupled with custom post types and taxonomies, an awesome CMS
- Essentially, your content lives in WordPress
Introduction
- So how does this content get into WordPress?
- If its a new site, you just enter via wp-admin. Simple?
- If there is existing content, what are your options?
- Importing
- Or using very patient people
- I am going to talk about importing
Importing into WordPress
- So what options are there?
- Tools > Import
- talk over, any questions?
Tools > Import
- Limited list of supported formats
- None are all that useful.
Tools > Import
- Blogger
- Blogroll
- Categories and Tags Converter
- LiveJournal
- Movable Type and TypePad
- RSS
- Tumblr
- WordPress
Tools > Import
- Most of this are:
- Blog focused
- Match very specific use cases.
- Livejournal?
- Other than WordPress, has anyone used these?
Tools > Import
- Only really useful importer is WordPress
- Uses WXR
- WordPress eXtended RSS
- aka XML
- Not much in the way of documentation
- WordPress exports it, that’s about it.
- Learn more about importing content via tools here: http://codex.wordpress.org/Importing_Content
So, me and WordPress.
- Work as a developer at an agency
- Very rarely use WordPress as a blog tool
- Use it primarily as a CMS
→ Custom post types- → Custom taxonomies
- → Custom fields (ACF etc)
- → Custom themes
- → etc
How useful is importing in this use case?- Lets find out.
And a Case Study
- Last year, Flint were approached by Sneaker Freaker to redevelop their aging website
- Existing website was a bespoke, proprietary CMS
- Their situation matched WordPress incredibly well
Sneaker Freaker
- Old site - Over 10k articles
- Even more media attached to articles
- Associated taxonomies
- Distinct content types
Sneaker Freaker
- Somehow all this content needed to get into WordPress
- Options:
- Leave the old site behind?
- Manually migrate?
- Tools > Import?
So, what did I do?
- Have I mentioned the pain, stress and grey hair this caused?
- Thinking I lacked a better option
- (and being stupidly idealistic about WordPress)
- I created a script to convert the content in WXR files.
- About a gigabytes worth.
So, what did I do?
- In short:
- Never do this.
- Ever.
- Find a better way
Seriously though
- Main issue, WXR (like all XML) is complex. Making it yourself is painful
- WXR import process is opaque
- Doing it for anything larger than a few megabytes in the browser is flakey
- Issues include: memory consumption, timeouts when pulling in external media etc
- Doing it via the command line is still a black box, but works eventually
Screw WXR, how about something else?
- In this instance, screw WXR.
- What to do instead?
- Insert content directly into the database? No.
- After hours of googling and venturing deep into the codex.
- There are better options…
- Mainly, core WordPress functions
What makes a WordPress site?
- This is slightly skewed to the cms use case
- Posts
- Post Meta (Custom fields etc)
- Taxonomies
- Media
- When importing, we need to be able to create all these.
First things first
The process I am going to describe requires the WordPress environment
Doesn’t fit into the template loaded model
Need to bootstrap WordPress in a separate file
<?php // import.php require( './wp-load.php' );
Have people done this before?
Creating a post
wp_insert_post
takes an array and inserts (or updates) a record in the wp_post table// Create post object as an array. // generally this would come from an external source. $my_post = array( 'post_title' => 'My post', 'post_content' => 'This is my post.', 'post_status' => 'publish', 'post_author' => 1, 'post_category' => array(8,39) ); // Insert the post into the database // Will return either the post ID or an instance of WP_Error // Make sure you capture the ID or stop on error. $post_id = wp_insert_post( $my_post, true );
Ref: http://codex.wordpress.org/Function_Reference/wp_insert_post
Updating a post
wp_insert_post
will update the post if ID is set in the array.wp_update_post
will only update the post.// Update post 37 $my_post = array( 'ID' => 37, 'post_content' => 'This is the updated content.' ); // Update the post into the database wp_update_post( $my_post );
Ref: http://codex.wordpress.org/Function_Reference/wp_update_post
Adding Post Meta
Now you’ve created a post, you might need to add some meta data to it.
There are functions for adding, or updating post meta, as with posts
// Accepts id of post, the meta key, the meta value // Final variable relates to whether the meta key is unique add_post_meta($post_id, 'length', '100m', true);
Ref: http://codex.wordpress.org/Function_Reference/add_post_meta
Updating Post Meta
In some instances, you might want to update post meta, if it exists.
if ( ! update_post_meta (7, 'fruit', 'banana' ) ) { // post meta for fruit, might end up as // banana, apple, etc add_post_meta( 7, 'fruit', 'banana' ); }
Ref: http://codex.wordpress.org/Function_Reference/update_post_meta
Creating a Taxonomy Term
wp_insert_term
will create a term for a specified taxonomy.Works for both built in taxonomies and custom taxonomies.
wp_insert_term( 'Apple', // the term 'product', // the taxonomy array( 'description'=> 'A yummy apple.', 'slug' => 'apple', 'parent'=> $parent_term_id ) ); // Returns an array of ids (term and term taxonomy) // Or error. Capture, as appropriate.
Ref: http://codex.wordpress.org/Function_Reference/wp_insert_term
Applying a Taxonomy to a post
wp_set_object_terms
allows you to associate a post object with a taxonomyCan be used for either built in taxonomies (tags, categories, formats etc)
Can also be used for custom taxonomies
// Add our post to categories. $cat_ids = array( 6, 8 ); wp_set_object_terms( $post_id, $cat_ids, 'category' ); // Associate our post to product. $product_ids = array( 7, 9); wp_set_object_terms( $post_id, $product_ids, 'product' );
Ref: http://codex.wordpress.org/Function_Reference/wp_set_object_terms
Media
- Media is always a difficult thing to import.
- You are not going to programmatically upload files?
- Always been a stumbling block due to hosting constraints etc
- Generally media will be images
- WordPress provides a function called
media_sideload_image
- Will download an image from a specified url and attachs it to a post
Media - One Solution for Images
media_sideload_image
allows us to grab an image from a urlFrom the codex
// Include relevant files. require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); $url = "http://s.wordpress.org/style/images/wp3-logo.png"; $post_id = 1; $desc = "The WordPress Logo"; $image = media_sideload_image($url, $post_id, $desc);
- Ref: http://codex.wordpress.org/Function_Reference/media_sideload_image
Conclusion
- Hopefully this gives you some ideas on how to handle situations that require you to import content
- Slides will be online tomorrow.
- Follow me on twitter: @navitronic
- Or watch the meetup group page
- Questions?
Thank you