WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to improve a bookmarks manager/theme I've built (http://bookmarkie.waterstreetgm.org).

Right now, each post is simply a link:

<a href="http://tumblr.everlane.com/page/4">Everlane Tumblr</a>. ie: that one single link is the entire content of the_content().

Easy enough and works great. Next, I want to be able to get a screenshot of the site via WordPress.com's mShots.

Problem is, I can't seem to extract that link from the_content() and feed it into the <img src="">.

If that wasn't clear, here's what I'm trying to do:

<img src="http://s.wordpress.com/mshots/v1/the-link-extracted-from-the_content()/?w=1024" />

I've tried severalvariations on this:

$content = get_the_content();
$link = preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $content, $match);

But I can't achieve the basic task of extracting that link from the_content(). At the moment, I simply get a formatted link output. I can't find a way to grab the url instead of grabbing the whole link.

Thanks for any suggestions.

share|improve this question
preg_match_all() returns the number of matches or FALSE on errror, your link will be in the $match array not in $link. – totels Nov 10 '12 at 1:04
up vote 2 down vote accepted

You're just looking in the wrong place:

php > $txt = '<a href="http://tumblr.everlane.com/page/4">Everlane Tumblr</a>';
php > $matches = array();
php > echo preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $txt, $matches);
php > print_r($matches);
    [0] => Array
            [0] => http://tumblr.everlane.com/page/4

    [1] => Array
            [0] => 4
share|improve this answer
So close! This seems to be outputting the href with the number 1 in front of it. Like this: 1http://google.com. I can't figure out where it's coming from. – saltcod Nov 10 '12 at 1:33
Turns out that number '1' was a lesson in return vs echo. The 1 was being echoed, while what I wanted was to return it to hand it to another function. Thanks for the help with this one! – saltcod Nov 10 '12 at 14:25

Instead of putting a link in the content area why don't you just put the URL in a custom field. Then you can construct the heading for the post and the tag for screenshot image within your template.

Even better would be to create a custom post type that didn't have a title or content area and just a custom meta box with a field for the URL.

share|improve this answer
Yeah, I could/should do an of these. But I've got 500+ posts/links in my bookmark manager now from years ago. – saltcod Nov 10 '12 at 0:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.