Solved

Creating Wordpress Featured Images from post images

Posted on 2014-12-17
5
137 Views
Last Modified: 2014-12-17
Hello!

I have a Wordpress site I inherited where all blog posts currently do not use the Featured Image function in Wordpress (latest) but instead the post starts with an image.

I'm hoping to do a few things, but I don't understand the Featured Image architecture well enough to understand what is going on here.

So I have the exported XML which I assume to be the best place to try and make a modification.

And i see each <item> being a post.

And each item includes the key for content:

<content:encoded><![CDATA[<img class="aligncenter size-full wp-image-7380" alt="widish-gifts-to-melt-away-stress" src="/wp-content/uploads/2014/12/widish-gifts-to-melt-away-stress.jpg" width="634" height="467" />

Is there a way to:

1. Strip out this image at the start of posts
2. Insert it as an associated Featured Image for the same item (post)
3. Save the revised XML

When I look at an item in the XML that does contain a featured image, the only difference I see is a thumbnail key, but if it is pulling an asset ID from that field, we may be stuck.

Any suggestions? Does anyone understand how this could work?

Thank you

Bill
0
Comment
Question by:billium99
  • 4
5 Comments
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40504930
So I have the exported XML which I assume to be the best place to try and make a modification.

And that would be your first mistake.  It isn't the best place, isn't even a place to make the modifications.  You want to work directly with the database table, specifically wp_posts and wp_postmeta.  The XML file can be altered but you would then have to delete all content on the site and reimport the XML.  This will cause tons of problems.  Always work in the database if you can.

1. Strip out this image at the start of posts

First, you will want to save the image path and the post ID somewhere.

I would do this using a regular expression to match the first <img> tag in a post and remove.  While you can do this with queries, my query-fu is iffy with this so I would work around that by dumping the wp_posts table to a SQL file and then using a good text editor to do the work, then replace wp_posts when done.

2. Insert it as an associated Featured Image for the same item (post)

Much harder to do.  Featured images are saved in wp_postmeta so you would need to create new entries in that table for each post. So the ID and path we saved in step one forms the basis of an Insert query that build each featured image in the database.

3. Save the revised XML

Moot, since the database is being directly altered.
0
 
LVL 1

Author Comment

by:billium99
ID: 40505206
I don't think I was clear. I'm migrating the content to a new website. New Wordpress instance and everything.

However, the current site is live and cannot be altered. The database tables cannot be altered. It's fine if I must do this all in the database on the new site, but that was why I was hoping to make the changes during the XML export/import process.

In any case, can you elaborate on "using a regular expression" in a text editor? Is this possible? We have hundreds of posts, so I'm not sure there is a clean way to get there in any sort of automated fashion.

Bill
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 40505268
I don't think I was clear. I'm migrating the content to a new website.

Ah-ha.  That changes things a little bit.  I still don't like using the XML file for this, though.  I talk about this in this article:

http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_10258-How-To-Move-A-WordPress-Site.html

The best way to move is to dump out the complete WordPress database to a text file and then use something like Sublime, Programmer's Notepad, or Notepad++ to do any search and replaces needed.  All of those programs (Sublime is paid, PN and N++ are free) support regular expression search and replace...the trick will be coming up with the correct regex to do this.

You're suffering from choices made prior to your taking this one.  Embedding a featured image into a post body is trivial.  Going the other way is not.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40505365
Bill, thanks for the (mostly undeserved) points.  If you want to ask additional followups, I'm happy to answer to the best of my ability...
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40505371
Aw, crap.  I'm an idiot.

Bill, look at this:

https://wordpress.org/plugins/quick-featured-images/

Assuming you migrate the site completely and correctly, that plugin should do what you ask.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question