Does anyone actually read these?
My latest update to the WordPress plugin is as well as grabbing the text from your Hive blog articles, it also now checks the content for references to images and makes a local copy on your WordPress site, switches out references to the new location, as well as adding those images to your Media Library.
This is important for a couple of reasons:
- Your Hive site or images might change, vanish, or move, so references would break.
- Having images local to your WordPress allows your WordPress settings to act upon them (for example you might use efficiency plugins that convert/thumbnail/etc uploaded images).
In some cases, there will be another need, and that is where the WordPress site is set up as a CMS backend, and articles will be delivered some other channel, or via cache/proxy front-end.
How it Works
To create a WordPress post is pretty simple using all built-in WordPress features:
// Set the post data $post_data = array( 'post_title' => 'My New Post', 'post_content' => 'This is the content of my new post.', 'post_status' => 'publish' ); // Insert the post into the database $post_id = wp_insert_post($post_data);
If you look over my past updates you can see how I am essentially generating the text by using a search and producing XML, which is then read back - ideal for this sort of purpose so long as the Hive database keeps serving.
Images are a little different because they are referenced in the text, it is not part of the payload.
Fortunately PHP has long been used for scraping. This is where you use your code to read other websites. This means we have access to HTTP routines, especially cURL, which can get both text and binary data.
Going in, however, I was a little worried how WordPress kind of sees images as "attachments" rather than as data.
For example, _thumbnail_id is a WordPress post meta key that is used to store the ID of the featured image for a post. WordPress uses post meta keys to store additional information about a post, such as the featured image, the author, the publish date, and so on.
The _thumbnail_id post meta key is used specifically to store the ID of the featured image for a post. When you set the featured image for a post using the WordPress editor, WordPress stores the ID of the featured image in the _thumbnail_id post meta key. You can then use this post meta key to retrieve the featured image for a post.
update_post_meta($post_id, '_thumbnail_id', $image_id); would add an image to a just-created article.
In this code, $post_id is the ID of the post that we are updating, '_thumbnail_id' is the post meta key, and $image_id is the ID of the featured image that we are setting for the post.
But our images already exist on the internet!
Enter "side loading" (like on Android phones I guess)
First, you would need to upload the image to the WordPress media library using the media_sideload_image() function. The media_sideload_image() function takes the image URL and the post ID as arguments, and it uploads the image to the media library and attaches it to the post.
To upload the image and attach it to the post, you would use the following code:
$image_id = media_sideload_image($image_url, $post_id);
In this code, $image_url is the URL of the image that you want to sideload, $post_id is the ID of the post that you want to attach the image to, and $image_id is the ID of the image that was uploaded to the media library.
I'm totally reading every word. Just discovered HiveWP existed thanks to @shadowspub who mentioned it on this week's #HiveSpace hosted by @alessandrawhite. Now I want to know "all the things" to see how I can use HiveWP on all of my Wordpress blogs.