Repaired my WordPress Blogs

Over the past few months, my blogs went down for one reason or the other:

  1. Blog – https://dushyant.ahuja.ws went down as there was a virus that had infected the blog – created thousands of random php files.
  2. PhotoBlog – https://photos.ahuja.ws went down as the YAPB plugin finally gave up – it hasn’t been maintained in years and the website started giving a critical error. Probably because I updated the PHP version to 8.

Blog

Somehow, I didn’t have any backups for my Blog – probably lost them in the many times I’ve reorganised my backups and lost my 10TB drive.

Anyways, the only option I had was to download the folder and setup a local instance of wordpress (setup on my server under docker). Was able to use one of the standard wordpress docker images and imported the sql backup into it. Surprise – surprise – the local version of the website came up immediately – though the images did not come in. That was a simple process of copying the jpg files from the ‘uploads’ folder from my backup into the new site, and ensuring that the permissions were setup correctly. The blog is now up and running – though some items like photo galleries and some plugins are not working.

The intent is to move this blog to a static site – either through hugo or pelican. For the time being, it’s working.

PhotoBlog

Making this work was slightly more involved as the moment I enabled the autofocus template that I’ve used for years, the website would go down. Secondly, I needed to convert all the posts from ‘YAPB’ to the wordpress featured image format. Searched the internet to see if there was a way to move from YAPB. Found a link to a plugin that the original writer of YAPB had written to move out of YAPB, however the link no longer works, and there was nothing on archive.org as well.

Next I found a PHP script that someone had written to export YAPB images – but couldn’t run it and it just didn’t work for me.

Moved on to ChatGPT – asked it to convert the PHP script into a wordpress plugin that I could import onto my site and convert all the YAPB images into featured images for the posts. After a couple of iterations, the plugin “worked” but made no changes.

Started to read the script to understand what it was doing and realised that since wordpress works primarily on SQL, I can probably update the mySQL database directly. So it was back to ChatGPT with the following prompt:

I'm using wordpress - I have a table with a list of all the image URLs and post ID where the image is to be inserted. Can you help write an SQL script that would take the image URLs and attach them to the post ID as the featured image

After a couple of iterations fine-tuning the code, I ended up with the following three commands

  1. Insert the image URLs from wp_yapbimage into wp_posts as attachments
INSERT INTO wp_posts (post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type, comment_count)
SELECT
    1, -- Default author ID, adjust if needed
    NOW(), -- Current timestamp
    NOW(), -- Current timestamp in GMT
    '', -- Empty content
    SUBSTRING_INDEX(wp_yapbimage.URI, '/', -1), -- Use the file name as the title
    '', -- Empty excerpt
    'inherit', -- Attachment post status
    'closed', -- Comment status
    'closed', -- Ping status
    '', -- Empty password
    SUBSTRING_INDEX(wp_yapbimage.URI, '/', -1), -- Use the file name as the slug
    '', -- Empty to_ping
    '', -- Empty pinged
    NOW(), -- Current timestamp for modified date
    NOW(), -- Current timestamp for modified GMT
    '', -- Empty content filtered
    wp_yapbimage.post_id, -- Link the attachment to its parent post
    wp_yapbimage.URI, -- The image URL
    0, -- Default menu order
    'attachment', -- Post type
    'image/jpeg', -- Mime type (adjust if necessary)
    0 -- Default comment count
FROM wp_yapbimage;
  1. Link the attachment as the featured image for posts
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT
    wp_yapbimage.post_id, -- The post ID to assign the featured image
    '_thumbnail_id', -- Meta key for featured image
    wp_posts.ID -- The ID of the attachment
FROM wp_posts
INNER JOIN wp_yapbimage ON wp_posts.guid = wp_yapbimage.URI
WHERE wp_posts.post_type = 'attachment';
  1. Had to insert this after testing and failing to get the thumbnails to display:
INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
SELECT
    posts.id,
    '_wp_attached_file',
    posts.post_title
FROM wp_posts posts
INNER JOIN wp_postmeta ON wp_postmeta.meta_value = posts.id AND wp_postmeta.meta_key='_thumbnail_id'
  1. Add the image into the post content
UPDATE wp_posts AS posts
INNER JOIN wp_yapbimage AS images ON posts.ID = images.post_id
SET posts.post_content = CONCAT(posts.post_content, '<img src="', images.URI, '" alt="', SUBSTRING_INDEX(images.URI, '/', -1), '">');

This allowed me to update all the posts with a featured image as well as the image in the post body.

  1. Installed the cubico theme. Though had to add the following to style.css to ensure the image stayed within the page and disable the featured image in the post page.
img {
  max-width: 90%;
  height: auto;
  margin: 0 auto;
}