Saturday, November 14th, 2009
A while back I wrote an article on Removing Images from a WordPress Post. Sebastian asked an interesting question; he wanted to remove everything but the images. This is actually pretty straightforward; here is how you do it:
.
.
.
<?php
$beforeEachImage = "<div>";
$afterEachImage = "</div>";
preg_match_all("/(<img [^>]*>)/",get_the_content(),$matches,PREG_PATTERN_ORDER);
for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) {
echo $beforeEachImage . $matches[1][$i] . $afterEachImage;
}
?>
.
.
.
Keep in mind this code needs to be in the WordPress Loop and you can control what is around each image using the variables beforeEachImage and afterEachImage above.
Monday, October 5th, 2009
From time to time I had have to move one installation of WordPress from one domain (let’s call it domain A) to a new domain (let’s call it domain B). Because WordPress embeds the domain all over the data schema (not faulting; simply disclosing) you have to make a lot of dB changes for the updated site location to function. Here is the quick and dirty way to move the data:
UPDATE wp_posts SET post_content = REPLACE(post_content,"[OLD_DOMAIN]","[NEW_DOMAIN]");
UPDATE wp_posts SET guid = REPLACE(guid,"[OLD_DOMAIN]","[NEW_DOMAIN]");
UPDATE wp_options SET option_value = REPLACE(option_value,"[OLD_DOMAIN]","[NEW_DOMAIN]");
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value,"[OLD_DOMAIN]","[NEW_DOMAIN]");
Finally, for the final bit of trickery if you are using Advanced Caching on the site there is a great place where the localized PATH is hard coded. Edit your new and updated path on file advanced-cache.php
vi wp-content/advanced-cache.php
Thursday, April 23rd, 2009
A while back I wrote a semi-popular post on removing images from a WordPress post — today I am revisiting it. The original solution used the_content() and the output buffer to remove the images out of the post. Now that I have used WordPress a bit longer and candidly had to use the solution again and thought “what was I thinking” I thought I would share the cleaner solution:
.
.
.
<?php echo preg_replace('/<img[^>]+./','',get_the_content()); ?>
.
.
.
Sunday, August 24th, 2008
Today I ran across a unique need to remove images from a WordPress post in a specific post loop. Because there is no way to do a “read more” excerpt while taking strict control of the raw content from the_content() I was limited to capturing and manipulating the content from PHP’s output buffer. My solution: obtain the output from the_content() and remove the image tags from the post using preg_replace().
Here is the solution:
.
.
.
<?php
ob_start();
the_content('Read the full post',true);
$postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
ob_end_clean();
echo $postOutput;
?>
.
.
.
EDIT: make sure you check out the updated version of this solution. Also, view my solution for removing everything but the images.